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#ifndef ASSEMBLER_META_H 17b1994897Sopenharmony_ci#define ASSEMBLER_META_H 18b1994897Sopenharmony_ci 19b1994897Sopenharmony_ci#include <memory> 20b1994897Sopenharmony_ci#include <stack> 21b1994897Sopenharmony_ci#include <tuple> 22b1994897Sopenharmony_ci#include <unordered_map> 23b1994897Sopenharmony_ci#include <unordered_set> 24b1994897Sopenharmony_ci#include <variant> 25b1994897Sopenharmony_ci#include <vector> 26b1994897Sopenharmony_ci 27b1994897Sopenharmony_ci#include "annotation.h" 28b1994897Sopenharmony_ci#include "modifiers.h" 29b1994897Sopenharmony_ci 30b1994897Sopenharmony_ci#include "assembly-type.h" 31b1994897Sopenharmony_ci 32b1994897Sopenharmony_cinamespace panda::pandasm { 33b1994897Sopenharmony_ci 34b1994897Sopenharmony_ciclass Metadata { 35b1994897Sopenharmony_cipublic: 36b1994897Sopenharmony_ci class Error { 37b1994897Sopenharmony_ci public: 38b1994897Sopenharmony_ci enum class Type { 39b1994897Sopenharmony_ci INVALID_VALUE, 40b1994897Sopenharmony_ci MISSING_ATTRIBUTE, 41b1994897Sopenharmony_ci MISSING_VALUE, 42b1994897Sopenharmony_ci MULTIPLE_ATTRIBUTE, 43b1994897Sopenharmony_ci UNEXPECTED_ATTRIBUTE, 44b1994897Sopenharmony_ci UNEXPECTED_VALUE, 45b1994897Sopenharmony_ci UNKNOWN_ATTRIBUTE 46b1994897Sopenharmony_ci }; 47b1994897Sopenharmony_ci 48b1994897Sopenharmony_ci Error(std::string msg, Type type) : msg_(std::move(msg)), type_(type) {} 49b1994897Sopenharmony_ci ~Error() = default; 50b1994897Sopenharmony_ci DEFAULT_MOVE_SEMANTIC(Error); 51b1994897Sopenharmony_ci DEFAULT_COPY_SEMANTIC(Error); 52b1994897Sopenharmony_ci 53b1994897Sopenharmony_ci std::string GetMessage() const 54b1994897Sopenharmony_ci { 55b1994897Sopenharmony_ci return msg_; 56b1994897Sopenharmony_ci } 57b1994897Sopenharmony_ci 58b1994897Sopenharmony_ci Type GetType() const 59b1994897Sopenharmony_ci { 60b1994897Sopenharmony_ci return type_; 61b1994897Sopenharmony_ci } 62b1994897Sopenharmony_ci 63b1994897Sopenharmony_ci private: 64b1994897Sopenharmony_ci std::string msg_; 65b1994897Sopenharmony_ci Type type_; 66b1994897Sopenharmony_ci }; 67b1994897Sopenharmony_ci 68b1994897Sopenharmony_ci Metadata() = default; 69b1994897Sopenharmony_ci 70b1994897Sopenharmony_ci virtual ~Metadata() = default; 71b1994897Sopenharmony_ci 72b1994897Sopenharmony_ci std::optional<Error> SetAttribute(const std::string_view &attribute) 73b1994897Sopenharmony_ci { 74b1994897Sopenharmony_ci auto err = Validate(attribute); 75b1994897Sopenharmony_ci if (err) { 76b1994897Sopenharmony_ci return err; 77b1994897Sopenharmony_ci } 78b1994897Sopenharmony_ci 79b1994897Sopenharmony_ci SetFlags(attribute); 80b1994897Sopenharmony_ci 81b1994897Sopenharmony_ci return Store(attribute); 82b1994897Sopenharmony_ci } 83b1994897Sopenharmony_ci 84b1994897Sopenharmony_ci void RemoveAttribute(const std::string &attribute) 85b1994897Sopenharmony_ci { 86b1994897Sopenharmony_ci RemoveFlags(attribute); 87b1994897Sopenharmony_ci 88b1994897Sopenharmony_ci set_attributes_.erase(attribute); 89b1994897Sopenharmony_ci } 90b1994897Sopenharmony_ci 91b1994897Sopenharmony_ci bool GetAttribute(const std::string &attribute) const 92b1994897Sopenharmony_ci { 93b1994897Sopenharmony_ci return set_attributes_.find(attribute) != set_attributes_.cend(); 94b1994897Sopenharmony_ci } 95b1994897Sopenharmony_ci 96b1994897Sopenharmony_ci std::optional<Error> SetAttributeValue(const std::string_view &attribute, const std::string_view &value) 97b1994897Sopenharmony_ci { 98b1994897Sopenharmony_ci auto err = Validate(attribute, value); 99b1994897Sopenharmony_ci if (err) { 100b1994897Sopenharmony_ci return err; 101b1994897Sopenharmony_ci } 102b1994897Sopenharmony_ci 103b1994897Sopenharmony_ci SetFlags(attribute, value); 104b1994897Sopenharmony_ci 105b1994897Sopenharmony_ci return StoreValue(attribute, value); 106b1994897Sopenharmony_ci } 107b1994897Sopenharmony_ci 108b1994897Sopenharmony_ci std::vector<std::string> GetAttributeValues(const std::string &attribute) const 109b1994897Sopenharmony_ci { 110b1994897Sopenharmony_ci auto it = attributes_.find(attribute); 111b1994897Sopenharmony_ci if (it == attributes_.cend()) { 112b1994897Sopenharmony_ci return {}; 113b1994897Sopenharmony_ci } 114b1994897Sopenharmony_ci 115b1994897Sopenharmony_ci return it->second; 116b1994897Sopenharmony_ci } 117b1994897Sopenharmony_ci 118b1994897Sopenharmony_ci std::optional<std::string> GetAttributeValue(const std::string &attribute) const 119b1994897Sopenharmony_ci { 120b1994897Sopenharmony_ci auto values = GetAttributeValues(attribute); 121b1994897Sopenharmony_ci if (!values.empty()) { 122b1994897Sopenharmony_ci return values.front(); 123b1994897Sopenharmony_ci } 124b1994897Sopenharmony_ci 125b1994897Sopenharmony_ci return {}; 126b1994897Sopenharmony_ci } 127b1994897Sopenharmony_ci 128b1994897Sopenharmony_ci const std::unordered_set<std::string> &GetBoolAttributes() const 129b1994897Sopenharmony_ci { 130b1994897Sopenharmony_ci return set_attributes_; 131b1994897Sopenharmony_ci } 132b1994897Sopenharmony_ci 133b1994897Sopenharmony_ci const std::unordered_map<std::string, std::vector<std::string>> &GetAttributes() const 134b1994897Sopenharmony_ci { 135b1994897Sopenharmony_ci return attributes_; 136b1994897Sopenharmony_ci } 137b1994897Sopenharmony_ci 138b1994897Sopenharmony_ci virtual std::optional<Error> ValidateData() 139b1994897Sopenharmony_ci { 140b1994897Sopenharmony_ci return {}; 141b1994897Sopenharmony_ci } 142b1994897Sopenharmony_ci 143b1994897Sopenharmony_ci DEFAULT_COPY_SEMANTIC(Metadata); 144b1994897Sopenharmony_ci 145b1994897Sopenharmony_ci DEFAULT_MOVE_SEMANTIC(Metadata); 146b1994897Sopenharmony_ci 147b1994897Sopenharmony_ciprotected: 148b1994897Sopenharmony_ci virtual std::optional<Error> Validate(const std::string_view &attribute) const = 0; 149b1994897Sopenharmony_ci 150b1994897Sopenharmony_ci virtual std::optional<Error> Validate(const std::string_view &attribute, 151b1994897Sopenharmony_ci const std::string_view &value) const = 0; 152b1994897Sopenharmony_ci 153b1994897Sopenharmony_ci virtual std::optional<Error> StoreValue(const std::string_view &attribute, const std::string_view &value) 154b1994897Sopenharmony_ci { 155b1994897Sopenharmony_ci std::string key(attribute); 156b1994897Sopenharmony_ci auto it = attributes_.find(key); 157b1994897Sopenharmony_ci if (it == attributes_.cend()) { 158b1994897Sopenharmony_ci std::tie(it, std::ignore) = attributes_.try_emplace(key); 159b1994897Sopenharmony_ci } 160b1994897Sopenharmony_ci 161b1994897Sopenharmony_ci it->second.emplace_back(value); 162b1994897Sopenharmony_ci 163b1994897Sopenharmony_ci return {}; 164b1994897Sopenharmony_ci } 165b1994897Sopenharmony_ci 166b1994897Sopenharmony_ci virtual std::optional<Error> Store(const std::string_view &attribute) 167b1994897Sopenharmony_ci { 168b1994897Sopenharmony_ci set_attributes_.emplace(attribute); 169b1994897Sopenharmony_ci 170b1994897Sopenharmony_ci return {}; 171b1994897Sopenharmony_ci } 172b1994897Sopenharmony_ci 173b1994897Sopenharmony_ci virtual void SetFlags(const std::string_view &attribute) = 0; 174b1994897Sopenharmony_ci 175b1994897Sopenharmony_ci virtual void SetFlags(const std::string_view &attribute, const std::string_view &value) = 0; 176b1994897Sopenharmony_ci 177b1994897Sopenharmony_ci virtual void RemoveFlags(const std::string_view &attribute) = 0; 178b1994897Sopenharmony_ci 179b1994897Sopenharmony_ci virtual void RemoveFlags(const std::string_view &attribute, const std::string_view &value) = 0; 180b1994897Sopenharmony_ci 181b1994897Sopenharmony_ci bool HasAttribute(const std::string_view &attribute) const 182b1994897Sopenharmony_ci { 183b1994897Sopenharmony_ci std::string key(attribute); 184b1994897Sopenharmony_ci return GetAttribute(key) || GetAttributeValue(key); 185b1994897Sopenharmony_ci } 186b1994897Sopenharmony_ci 187b1994897Sopenharmony_ci std::optional<Error> ValidateSize(const std::string_view &value) const; 188b1994897Sopenharmony_ci 189b1994897Sopenharmony_ciprivate: 190b1994897Sopenharmony_ci std::unordered_set<std::string> set_attributes_; 191b1994897Sopenharmony_ci std::unordered_map<std::string, std::vector<std::string>> attributes_; 192b1994897Sopenharmony_ci}; 193b1994897Sopenharmony_ci 194b1994897Sopenharmony_ciclass AnnotationMetadata : public Metadata { 195b1994897Sopenharmony_cipublic: 196b1994897Sopenharmony_ci const std::vector<AnnotationData> &GetAnnotations() const 197b1994897Sopenharmony_ci { 198b1994897Sopenharmony_ci return annotations_; 199b1994897Sopenharmony_ci } 200b1994897Sopenharmony_ci 201b1994897Sopenharmony_ci void SetAnnotations(std::vector<AnnotationData> &&annotations) 202b1994897Sopenharmony_ci { 203b1994897Sopenharmony_ci annotations_ = std::forward<std::vector<AnnotationData>>(annotations); 204b1994897Sopenharmony_ci } 205b1994897Sopenharmony_ci 206b1994897Sopenharmony_ci void AddAnnotations(const std::vector<AnnotationData> &annotations) 207b1994897Sopenharmony_ci { 208b1994897Sopenharmony_ci annotations_.insert(annotations_.end(), annotations.begin(), annotations.end()); 209b1994897Sopenharmony_ci } 210b1994897Sopenharmony_ci 211b1994897Sopenharmony_ci void DeleteAnnotationElementByName(std::string_view annotation_name, std::string_view annotation_elem_name) 212b1994897Sopenharmony_ci { 213b1994897Sopenharmony_ci auto annotation_iter = std::find_if(annotations_.begin(), annotations_.end(), 214b1994897Sopenharmony_ci [&](pandasm::AnnotationData &annotation) -> bool { 215b1994897Sopenharmony_ci return annotation.GetName() == annotation_name; 216b1994897Sopenharmony_ci }); 217b1994897Sopenharmony_ci if (annotation_iter != annotations_.end()) { 218b1994897Sopenharmony_ci annotation_iter->DeleteAnnotationElementByName(annotation_elem_name); 219b1994897Sopenharmony_ci } 220b1994897Sopenharmony_ci } 221b1994897Sopenharmony_ci 222b1994897Sopenharmony_ci void DeleteAnnotationByName(const std::string_view &annotation_name) 223b1994897Sopenharmony_ci { 224b1994897Sopenharmony_ci auto annotation_iter = std::find_if(annotations_.begin(), annotations_.end(), 225b1994897Sopenharmony_ci [&](pandasm::AnnotationData &annotation) -> bool { 226b1994897Sopenharmony_ci return annotation.GetName() == annotation_name; 227b1994897Sopenharmony_ci }); 228b1994897Sopenharmony_ci if (annotation_iter != annotations_.end()) { 229b1994897Sopenharmony_ci (void)annotations_.erase(annotation_iter); 230b1994897Sopenharmony_ci } 231b1994897Sopenharmony_ci } 232b1994897Sopenharmony_ci 233b1994897Sopenharmony_ci void AddAnnotationElementByName(const std::string_view &annotation_name, AnnotationElement &&element) 234b1994897Sopenharmony_ci { 235b1994897Sopenharmony_ci auto annotation_iter = std::find_if(annotations_.begin(), annotations_.end(), 236b1994897Sopenharmony_ci [&](pandasm::AnnotationData &annotation) -> bool { 237b1994897Sopenharmony_ci return annotation.GetName() == annotation_name; 238b1994897Sopenharmony_ci }); 239b1994897Sopenharmony_ci if (annotation_iter != annotations_.end()) { 240b1994897Sopenharmony_ci annotation_iter->AddElement(std::move(element)); 241b1994897Sopenharmony_ci } 242b1994897Sopenharmony_ci } 243b1994897Sopenharmony_ci 244b1994897Sopenharmony_ci void SetOrAddAnnotationElementByIndex(size_t anno_idx, size_t ele_idx, AnnotationElement &&element) 245b1994897Sopenharmony_ci { 246b1994897Sopenharmony_ci ASSERT(anno_idx < annotations_.size()); 247b1994897Sopenharmony_ci annotations_[anno_idx].SetOrAddElementByIndex(ele_idx, std::forward<AnnotationElement>(element)); 248b1994897Sopenharmony_ci } 249b1994897Sopenharmony_ci 250b1994897Sopenharmony_ci std::optional<Error> ValidateData() override; 251b1994897Sopenharmony_ci 252b1994897Sopenharmony_ciprotected: 253b1994897Sopenharmony_ci std::optional<Error> Store(const std::string_view &attribute) override; 254b1994897Sopenharmony_ci 255b1994897Sopenharmony_ci std::optional<Error> StoreValue(const std::string_view &attribute, const std::string_view &value) override; 256b1994897Sopenharmony_ci 257b1994897Sopenharmony_ci virtual bool IsAnnotationRecordAttribute([[maybe_unused]] const std::string_view &attribute) const 258b1994897Sopenharmony_ci { 259b1994897Sopenharmony_ci return false; 260b1994897Sopenharmony_ci } 261b1994897Sopenharmony_ci 262b1994897Sopenharmony_ci virtual bool IsAnnotationIdAttribute([[maybe_unused]] const std::string_view &attribute) const 263b1994897Sopenharmony_ci { 264b1994897Sopenharmony_ci return false; 265b1994897Sopenharmony_ci } 266b1994897Sopenharmony_ci 267b1994897Sopenharmony_ci virtual bool IsAnnotationElementTypeAttribute([[maybe_unused]] const std::string_view &attribute) const 268b1994897Sopenharmony_ci { 269b1994897Sopenharmony_ci return false; 270b1994897Sopenharmony_ci } 271b1994897Sopenharmony_ci 272b1994897Sopenharmony_ci virtual bool IsAnnotationElementArrayComponentTypeAttribute( 273b1994897Sopenharmony_ci [[maybe_unused]] const std::string_view &attribute) const 274b1994897Sopenharmony_ci { 275b1994897Sopenharmony_ci return false; 276b1994897Sopenharmony_ci } 277b1994897Sopenharmony_ci 278b1994897Sopenharmony_ci virtual bool IsAnnotationElementNameAttribute([[maybe_unused]] const std::string_view &attribute) const 279b1994897Sopenharmony_ci { 280b1994897Sopenharmony_ci return false; 281b1994897Sopenharmony_ci } 282b1994897Sopenharmony_ci 283b1994897Sopenharmony_ci virtual bool IsAnnotationElementValueAttribute([[maybe_unused]] const std::string_view &attribute) const 284b1994897Sopenharmony_ci { 285b1994897Sopenharmony_ci return false; 286b1994897Sopenharmony_ci } 287b1994897Sopenharmony_ci 288b1994897Sopenharmony_ciprivate: 289b1994897Sopenharmony_ci class AnnotationElementBuilder { 290b1994897Sopenharmony_ci public: 291b1994897Sopenharmony_ci void Initialize(const std::string_view &name) 292b1994897Sopenharmony_ci { 293b1994897Sopenharmony_ci name_ = name; 294b1994897Sopenharmony_ci is_initialized_ = true; 295b1994897Sopenharmony_ci } 296b1994897Sopenharmony_ci 297b1994897Sopenharmony_ci void Reset() 298b1994897Sopenharmony_ci { 299b1994897Sopenharmony_ci name_.clear(); 300b1994897Sopenharmony_ci values_.clear(); 301b1994897Sopenharmony_ci type_ = {}; 302b1994897Sopenharmony_ci component_type_ = {}; 303b1994897Sopenharmony_ci is_initialized_ = false; 304b1994897Sopenharmony_ci } 305b1994897Sopenharmony_ci 306b1994897Sopenharmony_ci void SetType(Value::Type type) 307b1994897Sopenharmony_ci { 308b1994897Sopenharmony_ci type_ = type; 309b1994897Sopenharmony_ci } 310b1994897Sopenharmony_ci 311b1994897Sopenharmony_ci void SetComponentType(Value::Type type) 312b1994897Sopenharmony_ci { 313b1994897Sopenharmony_ci ASSERT(type != Value::Type::ARRAY); 314b1994897Sopenharmony_ci component_type_ = type; 315b1994897Sopenharmony_ci } 316b1994897Sopenharmony_ci 317b1994897Sopenharmony_ci std::optional<Error> AddValue( 318b1994897Sopenharmony_ci const std::string_view &value, 319b1994897Sopenharmony_ci const std::unordered_map<std::string, std::unique_ptr<AnnotationData>> &annotation_id_map); 320b1994897Sopenharmony_ci 321b1994897Sopenharmony_ci AnnotationElement CreateAnnotationElement() 322b1994897Sopenharmony_ci { 323b1994897Sopenharmony_ci if (IsArray()) { 324b1994897Sopenharmony_ci return AnnotationElement(name_, 325b1994897Sopenharmony_ci std::make_unique<ArrayValue>(component_type_.value(), std::move(values_))); 326b1994897Sopenharmony_ci } 327b1994897Sopenharmony_ci 328b1994897Sopenharmony_ci return AnnotationElement(name_, std::make_unique<ScalarValue>(values_.front())); 329b1994897Sopenharmony_ci } 330b1994897Sopenharmony_ci 331b1994897Sopenharmony_ci bool IsArray() const 332b1994897Sopenharmony_ci { 333b1994897Sopenharmony_ci return type_.value() == Value::Type::ARRAY; 334b1994897Sopenharmony_ci } 335b1994897Sopenharmony_ci 336b1994897Sopenharmony_ci bool IsTypeSet() const 337b1994897Sopenharmony_ci { 338b1994897Sopenharmony_ci return type_.has_value(); 339b1994897Sopenharmony_ci } 340b1994897Sopenharmony_ci 341b1994897Sopenharmony_ci bool IsComponentTypeSet() const 342b1994897Sopenharmony_ci { 343b1994897Sopenharmony_ci return component_type_.has_value(); 344b1994897Sopenharmony_ci } 345b1994897Sopenharmony_ci 346b1994897Sopenharmony_ci bool IsInitialized() const 347b1994897Sopenharmony_ci { 348b1994897Sopenharmony_ci return is_initialized_; 349b1994897Sopenharmony_ci } 350b1994897Sopenharmony_ci 351b1994897Sopenharmony_ci bool IsCompleted() const 352b1994897Sopenharmony_ci { 353b1994897Sopenharmony_ci if (!IsTypeSet()) { 354b1994897Sopenharmony_ci return false; 355b1994897Sopenharmony_ci } 356b1994897Sopenharmony_ci 357b1994897Sopenharmony_ci if (IsArray() && !IsComponentTypeSet()) { 358b1994897Sopenharmony_ci return false; 359b1994897Sopenharmony_ci } 360b1994897Sopenharmony_ci 361b1994897Sopenharmony_ci if (!IsArray() && values_.empty()) { 362b1994897Sopenharmony_ci return false; 363b1994897Sopenharmony_ci } 364b1994897Sopenharmony_ci 365b1994897Sopenharmony_ci return true; 366b1994897Sopenharmony_ci } 367b1994897Sopenharmony_ci 368b1994897Sopenharmony_ci private: 369b1994897Sopenharmony_ci bool is_initialized_ {false}; 370b1994897Sopenharmony_ci std::string name_; 371b1994897Sopenharmony_ci std::optional<Value::Type> type_; 372b1994897Sopenharmony_ci std::optional<Value::Type> component_type_; 373b1994897Sopenharmony_ci std::vector<ScalarValue> values_; 374b1994897Sopenharmony_ci }; 375b1994897Sopenharmony_ci 376b1994897Sopenharmony_ci class AnnotationBuilder { 377b1994897Sopenharmony_ci public: 378b1994897Sopenharmony_ci void Initialize(const std::string_view &name) 379b1994897Sopenharmony_ci { 380b1994897Sopenharmony_ci name_ = name; 381b1994897Sopenharmony_ci is_initialized_ = true; 382b1994897Sopenharmony_ci } 383b1994897Sopenharmony_ci 384b1994897Sopenharmony_ci void Reset() 385b1994897Sopenharmony_ci { 386b1994897Sopenharmony_ci name_.clear(); 387b1994897Sopenharmony_ci elements_.clear(); 388b1994897Sopenharmony_ci id_ = {}; 389b1994897Sopenharmony_ci is_initialized_ = false; 390b1994897Sopenharmony_ci } 391b1994897Sopenharmony_ci 392b1994897Sopenharmony_ci void SetId(const std::string_view &id) 393b1994897Sopenharmony_ci { 394b1994897Sopenharmony_ci id_ = id; 395b1994897Sopenharmony_ci } 396b1994897Sopenharmony_ci 397b1994897Sopenharmony_ci std::string GetId() const 398b1994897Sopenharmony_ci { 399b1994897Sopenharmony_ci ASSERT(HasId()); 400b1994897Sopenharmony_ci return id_.value(); 401b1994897Sopenharmony_ci } 402b1994897Sopenharmony_ci 403b1994897Sopenharmony_ci void AddElement(AnnotationElement &&element) 404b1994897Sopenharmony_ci { 405b1994897Sopenharmony_ci elements_.push_back(std::forward<AnnotationElement>(element)); 406b1994897Sopenharmony_ci } 407b1994897Sopenharmony_ci 408b1994897Sopenharmony_ci std::unique_ptr<AnnotationData> CreateAnnotationData() 409b1994897Sopenharmony_ci { 410b1994897Sopenharmony_ci return std::make_unique<AnnotationData>(name_, std::move(elements_)); 411b1994897Sopenharmony_ci }; 412b1994897Sopenharmony_ci 413b1994897Sopenharmony_ci void AddAnnnotationDataToVector(std::vector<AnnotationData> *annotations) 414b1994897Sopenharmony_ci { 415b1994897Sopenharmony_ci annotations->emplace_back(name_, std::move(elements_)); 416b1994897Sopenharmony_ci } 417b1994897Sopenharmony_ci 418b1994897Sopenharmony_ci bool HasId() const 419b1994897Sopenharmony_ci { 420b1994897Sopenharmony_ci return id_.has_value(); 421b1994897Sopenharmony_ci } 422b1994897Sopenharmony_ci 423b1994897Sopenharmony_ci bool IsInitialized() const 424b1994897Sopenharmony_ci { 425b1994897Sopenharmony_ci return is_initialized_; 426b1994897Sopenharmony_ci } 427b1994897Sopenharmony_ci 428b1994897Sopenharmony_ci private: 429b1994897Sopenharmony_ci std::string name_; 430b1994897Sopenharmony_ci std::optional<std::string> id_; 431b1994897Sopenharmony_ci std::vector<AnnotationElement> elements_; 432b1994897Sopenharmony_ci bool is_initialized_ {false}; 433b1994897Sopenharmony_ci }; 434b1994897Sopenharmony_ci 435b1994897Sopenharmony_ci std::optional<Metadata::Error> MeetExpRecordAttribute(const std::string_view &attribute, 436b1994897Sopenharmony_ci const std::string_view &value); 437b1994897Sopenharmony_ci std::optional<Metadata::Error> MeetExpIdAttribute(const std::string_view &attribute, const std::string_view &value); 438b1994897Sopenharmony_ci std::optional<Metadata::Error> MeetExpElementNameAttribute(const std::string_view &attribute, 439b1994897Sopenharmony_ci const std::string_view &value); 440b1994897Sopenharmony_ci std::optional<Metadata::Error> MeetExpElementTypeAttribute(const std::string_view &attribute, 441b1994897Sopenharmony_ci const std::string_view &value); 442b1994897Sopenharmony_ci std::optional<Metadata::Error> MeetExpElementArrayComponentTypeAttribute(const std::string_view &attribute, 443b1994897Sopenharmony_ci const std::string_view &value); 444b1994897Sopenharmony_ci std::optional<Metadata::Error> MeetExpElementValueAttribute(const std::string_view &attribute, 445b1994897Sopenharmony_ci const std::string_view &value); 446b1994897Sopenharmony_ci 447b1994897Sopenharmony_ci void InitializeAnnotationBuilder(const std::string_view &name) 448b1994897Sopenharmony_ci { 449b1994897Sopenharmony_ci if (IsParseAnnotation()) { 450b1994897Sopenharmony_ci ResetAnnotationBuilder(); 451b1994897Sopenharmony_ci } 452b1994897Sopenharmony_ci 453b1994897Sopenharmony_ci annotation_builder_.Initialize(name); 454b1994897Sopenharmony_ci } 455b1994897Sopenharmony_ci 456b1994897Sopenharmony_ci void ResetAnnotationBuilder() 457b1994897Sopenharmony_ci { 458b1994897Sopenharmony_ci ASSERT(IsParseAnnotation()); 459b1994897Sopenharmony_ci 460b1994897Sopenharmony_ci if (IsParseAnnotationElement() && annotation_element_builder_.IsCompleted()) { 461b1994897Sopenharmony_ci ResetAnnotationElementBuilder(); 462b1994897Sopenharmony_ci } 463b1994897Sopenharmony_ci 464b1994897Sopenharmony_ci if (annotation_builder_.HasId()) { 465b1994897Sopenharmony_ci id_map_.insert({annotation_builder_.GetId(), annotation_builder_.CreateAnnotationData()}); 466b1994897Sopenharmony_ci } else { 467b1994897Sopenharmony_ci annotation_builder_.AddAnnnotationDataToVector(&annotations_); 468b1994897Sopenharmony_ci } 469b1994897Sopenharmony_ci 470b1994897Sopenharmony_ci annotation_builder_.Reset(); 471b1994897Sopenharmony_ci } 472b1994897Sopenharmony_ci 473b1994897Sopenharmony_ci bool IsParseAnnotation() const 474b1994897Sopenharmony_ci { 475b1994897Sopenharmony_ci return annotation_builder_.IsInitialized(); 476b1994897Sopenharmony_ci } 477b1994897Sopenharmony_ci 478b1994897Sopenharmony_ci void InitializeAnnotationElementBuilder(const std::string_view &name) 479b1994897Sopenharmony_ci { 480b1994897Sopenharmony_ci if (IsParseAnnotationElement() && annotation_element_builder_.IsCompleted()) { 481b1994897Sopenharmony_ci ResetAnnotationElementBuilder(); 482b1994897Sopenharmony_ci } 483b1994897Sopenharmony_ci 484b1994897Sopenharmony_ci annotation_element_builder_.Initialize(name); 485b1994897Sopenharmony_ci } 486b1994897Sopenharmony_ci 487b1994897Sopenharmony_ci void ResetAnnotationElementBuilder() 488b1994897Sopenharmony_ci { 489b1994897Sopenharmony_ci ASSERT(IsParseAnnotationElement()); 490b1994897Sopenharmony_ci ASSERT(annotation_element_builder_.IsCompleted()); 491b1994897Sopenharmony_ci 492b1994897Sopenharmony_ci annotation_builder_.AddElement(annotation_element_builder_.CreateAnnotationElement()); 493b1994897Sopenharmony_ci 494b1994897Sopenharmony_ci annotation_element_builder_.Reset(); 495b1994897Sopenharmony_ci } 496b1994897Sopenharmony_ci 497b1994897Sopenharmony_ci bool IsParseAnnotationElement() const 498b1994897Sopenharmony_ci { 499b1994897Sopenharmony_ci return annotation_element_builder_.IsInitialized(); 500b1994897Sopenharmony_ci } 501b1994897Sopenharmony_ci 502b1994897Sopenharmony_ci AnnotationBuilder annotation_builder_; 503b1994897Sopenharmony_ci AnnotationElementBuilder annotation_element_builder_; 504b1994897Sopenharmony_ci std::vector<AnnotationData> annotations_; 505b1994897Sopenharmony_ci std::unordered_map<std::string, std::unique_ptr<AnnotationData>> id_map_; 506b1994897Sopenharmony_ci}; 507b1994897Sopenharmony_ci 508b1994897Sopenharmony_ciclass ItemMetadata : public AnnotationMetadata { 509b1994897Sopenharmony_cipublic: 510b1994897Sopenharmony_ci uint32_t GetAccessFlags() const 511b1994897Sopenharmony_ci { 512b1994897Sopenharmony_ci return access_flags_; 513b1994897Sopenharmony_ci } 514b1994897Sopenharmony_ci 515b1994897Sopenharmony_ci void SetAccessFlags(uint32_t access_flags) 516b1994897Sopenharmony_ci { 517b1994897Sopenharmony_ci access_flags_ = access_flags; 518b1994897Sopenharmony_ci } 519b1994897Sopenharmony_ci 520b1994897Sopenharmony_ci void AddAccessFlags(uint32_t access_flags) 521b1994897Sopenharmony_ci { 522b1994897Sopenharmony_ci access_flags_ = access_flags_ | access_flags; 523b1994897Sopenharmony_ci } 524b1994897Sopenharmony_ci 525b1994897Sopenharmony_ci bool IsForeign() const; 526b1994897Sopenharmony_ci 527b1994897Sopenharmony_ciprivate: 528b1994897Sopenharmony_ci uint32_t access_flags_ {0}; 529b1994897Sopenharmony_ci}; 530b1994897Sopenharmony_ci 531b1994897Sopenharmony_ciclass RecordMetadata : public ItemMetadata { 532b1994897Sopenharmony_cipublic: 533b1994897Sopenharmony_ci virtual std::string GetBase() const; 534b1994897Sopenharmony_ci 535b1994897Sopenharmony_ci virtual std::vector<std::string> GetInterfaces() const; 536b1994897Sopenharmony_ci 537b1994897Sopenharmony_ci virtual bool IsAnnotation() const; 538b1994897Sopenharmony_ci 539b1994897Sopenharmony_ci virtual bool IsRuntimeAnnotation() const; 540b1994897Sopenharmony_ci 541b1994897Sopenharmony_ci virtual bool IsTypeAnnotation() const; 542b1994897Sopenharmony_ci 543b1994897Sopenharmony_ci virtual bool IsRuntimeTypeAnnotation() const; 544b1994897Sopenharmony_ci 545b1994897Sopenharmony_ciprotected: 546b1994897Sopenharmony_ci std::optional<Error> Validate(const std::string_view &attribute) const override; 547b1994897Sopenharmony_ci 548b1994897Sopenharmony_ci std::optional<Error> Validate(const std::string_view &attribute, const std::string_view &value) const override; 549b1994897Sopenharmony_ci 550b1994897Sopenharmony_ci void SetFlags(const std::string_view &attribute) override; 551b1994897Sopenharmony_ci 552b1994897Sopenharmony_ci void SetFlags(const std::string_view &attribute, const std::string_view &value) override; 553b1994897Sopenharmony_ci 554b1994897Sopenharmony_ci void RemoveFlags(const std::string_view &attribute) override; 555b1994897Sopenharmony_ci 556b1994897Sopenharmony_ci void RemoveFlags(const std::string_view &attribute, const std::string_view &value) override; 557b1994897Sopenharmony_ci}; 558b1994897Sopenharmony_ci 559b1994897Sopenharmony_ciclass FieldMetadata : public ItemMetadata { 560b1994897Sopenharmony_cipublic: 561b1994897Sopenharmony_ci void SetFieldType(const Type &type) 562b1994897Sopenharmony_ci { 563b1994897Sopenharmony_ci field_type_ = type; 564b1994897Sopenharmony_ci } 565b1994897Sopenharmony_ci 566b1994897Sopenharmony_ci Type GetFieldType() const 567b1994897Sopenharmony_ci { 568b1994897Sopenharmony_ci return field_type_; 569b1994897Sopenharmony_ci } 570b1994897Sopenharmony_ci 571b1994897Sopenharmony_ci void SetValue(const ScalarValue &value) 572b1994897Sopenharmony_ci { 573b1994897Sopenharmony_ci value_ = value; 574b1994897Sopenharmony_ci } 575b1994897Sopenharmony_ci 576b1994897Sopenharmony_ci std::optional<ScalarValue> GetValue() const 577b1994897Sopenharmony_ci { 578b1994897Sopenharmony_ci return value_; 579b1994897Sopenharmony_ci } 580b1994897Sopenharmony_ci 581b1994897Sopenharmony_ciprotected: 582b1994897Sopenharmony_ci std::optional<Error> StoreValue(const std::string_view &attribute, const std::string_view &value) override; 583b1994897Sopenharmony_ci 584b1994897Sopenharmony_ci std::optional<Error> Validate(const std::string_view &attribute) const override; 585b1994897Sopenharmony_ci 586b1994897Sopenharmony_ci std::optional<Error> Validate(const std::string_view &attribute, const std::string_view &value) const override; 587b1994897Sopenharmony_ci 588b1994897Sopenharmony_ci void SetFlags(const std::string_view &attribute) override; 589b1994897Sopenharmony_ci 590b1994897Sopenharmony_ci void SetFlags(const std::string_view &attribute, const std::string_view &value) override; 591b1994897Sopenharmony_ci 592b1994897Sopenharmony_ci void RemoveFlags(const std::string_view &attribute) override; 593b1994897Sopenharmony_ci 594b1994897Sopenharmony_ci void RemoveFlags(const std::string_view &attribute, const std::string_view &value) override; 595b1994897Sopenharmony_ci 596b1994897Sopenharmony_ci virtual bool IsValueAttribute(const std::string_view &attribute) 597b1994897Sopenharmony_ci { 598b1994897Sopenharmony_ci return attribute == "value"; 599b1994897Sopenharmony_ci } 600b1994897Sopenharmony_ci 601b1994897Sopenharmony_ciprivate: 602b1994897Sopenharmony_ci Type field_type_; 603b1994897Sopenharmony_ci std::optional<ScalarValue> value_; 604b1994897Sopenharmony_ci}; 605b1994897Sopenharmony_ci 606b1994897Sopenharmony_ciclass FunctionMetadata : public ItemMetadata { 607b1994897Sopenharmony_cipublic: 608b1994897Sopenharmony_ci virtual bool IsCtor() const; 609b1994897Sopenharmony_ci 610b1994897Sopenharmony_ci virtual bool IsCctor() const; 611b1994897Sopenharmony_ci 612b1994897Sopenharmony_ciprotected: 613b1994897Sopenharmony_ci std::optional<Error> Validate(const std::string_view &attribute) const override; 614b1994897Sopenharmony_ci 615b1994897Sopenharmony_ci std::optional<Error> Validate(const std::string_view &attribute, const std::string_view &value) const override; 616b1994897Sopenharmony_ci 617b1994897Sopenharmony_ci void SetFlags(const std::string_view &attribute) override; 618b1994897Sopenharmony_ci 619b1994897Sopenharmony_ci void SetFlags(const std::string_view &attribute, const std::string_view &value) override; 620b1994897Sopenharmony_ci 621b1994897Sopenharmony_ci void RemoveFlags(const std::string_view &attribute) override; 622b1994897Sopenharmony_ci 623b1994897Sopenharmony_ci void RemoveFlags(const std::string_view &attribute, const std::string_view &value) override; 624b1994897Sopenharmony_ci}; 625b1994897Sopenharmony_ci 626b1994897Sopenharmony_ciclass ParamMetadata : public AnnotationMetadata { 627b1994897Sopenharmony_ciprotected: 628b1994897Sopenharmony_ci std::optional<Error> Validate(const std::string_view &attribute) const override; 629b1994897Sopenharmony_ci 630b1994897Sopenharmony_ci std::optional<Error> Validate(const std::string_view &attribute, const std::string_view &value) const override; 631b1994897Sopenharmony_ci 632b1994897Sopenharmony_ci void SetFlags(const std::string_view &attribute) override; 633b1994897Sopenharmony_ci 634b1994897Sopenharmony_ci void SetFlags(const std::string_view &attribute, const std::string_view &value) override; 635b1994897Sopenharmony_ci 636b1994897Sopenharmony_ci void RemoveFlags(const std::string_view &attribute) override; 637b1994897Sopenharmony_ci 638b1994897Sopenharmony_ci void RemoveFlags(const std::string_view &attribute, const std::string_view &value) override; 639b1994897Sopenharmony_ci}; 640b1994897Sopenharmony_ci 641b1994897Sopenharmony_ci} // namespace panda::pandasm 642b1994897Sopenharmony_ci 643b1994897Sopenharmony_ci#endif // ASSEMBLER_META_H 644