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