1 /* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef OHOS_IDL_ASTUNIONTYPE_H 17 #define OHOS_IDL_ASTUNIONTYPE_H 18 19 #include <tuple> 20 #include <vector> 21 22 #include "ast/ast_attribute.h" 23 #include "ast/ast_type.h" 24 #include "util/autoptr.h" 25 26 namespace OHOS { 27 namespace Idl { 28 class ASTUnionType : public ASTType { 29 public: ASTUnionType()30 ASTUnionType() : ASTType(true), attr_(new ASTAttr()), members_() {} 31 SetName(const std::string & name)32 inline void SetName(const std::string &name) override 33 { 34 name_ = name; 35 } 36 GetName()37 inline std::string GetName() override 38 { 39 return name_; 40 } 41 SetAttribute(const AutoPtr<ASTAttr> & attr)42 inline void SetAttribute(const AutoPtr<ASTAttr> &attr) 43 { 44 if (attr != nullptr) { 45 attr_ = attr; 46 } 47 } 48 IsFull()49 inline bool IsFull() 50 { 51 return attr_ != nullptr ? attr_->HasValue(ASTAttr::FULL) : false; 52 } 53 IsLite()54 inline bool IsLite() 55 { 56 return attr_ != nullptr ? attr_->HasValue(ASTAttr::LITE) : false; 57 } 58 59 void AddMember(const AutoPtr<ASTType> &typeName, std::string name); 60 GetMemberNumber()61 inline size_t GetMemberNumber() 62 { 63 return members_.size(); 64 } 65 GetMemberName(size_t index)66 inline std::string GetMemberName(size_t index) 67 { 68 if (index >= members_.size()) { 69 return std::string(""); 70 } 71 return std::get<0>(members_[index]); 72 } 73 GetMemberType(size_t index)74 inline AutoPtr<ASTType> GetMemberType(size_t index) 75 { 76 if (index >= members_.size()) { 77 return nullptr; 78 } 79 return std::get<1>(members_[index]); 80 } 81 82 std::string GetSignature() override; 83 84 bool IsUnionType() override; 85 86 std::string Dump(const std::string &prefix) override; 87 88 TypeKind GetTypeKind() override; 89 private: 90 AutoPtr<ASTAttr> attr_; 91 std::vector<std::tuple<std::string, AutoPtr<ASTType>>> members_; 92 }; 93 } // namespace Idl 94 } // namespace OHOS 95 96 #endif // OHOS_IDL_ASTUNIONTYPE_H