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