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_ASTENUMTYPE_H 17 #define OHOS_IDL_ASTENUMTYPE_H 18 19 #include "ast/ast_attribute.h" 20 #include "ast/ast_expr.h" 21 #include "ast/ast_type.h" 22 #include "util/autoptr.h" 23 24 #include <vector> 25 26 namespace OHOS { 27 namespace Idl { 28 class ASTEnumValue : public ASTNode { 29 public: ASTEnumValue(const std::string & name)30 explicit ASTEnumValue(const std::string &name) : mName_(name), value_(nullptr) {} 31 ~ASTEnumValue()32 inline ~ASTEnumValue() override {} 33 GetName()34 inline std::string GetName() 35 { 36 return mName_; 37 } 38 SetType(const AutoPtr<ASTType> & type)39 inline void SetType(const AutoPtr<ASTType> &type) 40 { 41 mType_ = type; 42 } 43 GetType()44 inline AutoPtr<ASTType> GetType() 45 { 46 return mType_; 47 } 48 SetExprValue(const AutoPtr<ASTExpr> & value)49 inline void SetExprValue(const AutoPtr<ASTExpr> &value) 50 { 51 value_ = value; 52 } 53 GetExprValue()54 inline AutoPtr<ASTExpr> GetExprValue() 55 { 56 return value_; 57 } 58 59 private: 60 std::string mName_; 61 AutoPtr<ASTType> mType_; 62 AutoPtr<ASTExpr> value_; 63 }; 64 65 class ASTEnumType : public ASTType { 66 public: ASTEnumType()67 ASTEnumType() : ASTType(true), attr_(new ASTAttr()), baseType_(), members_() {} 68 SetName(const std::string & name)69 inline void SetName(const std::string &name) override 70 { 71 name_ = name; 72 } 73 GetName()74 inline std::string GetName() override 75 { 76 return name_; 77 } 78 SetAttribute(const AutoPtr<ASTAttr> & attr)79 inline void SetAttribute(const AutoPtr<ASTAttr> &attr) 80 { 81 if (attr != nullptr) { 82 attr_ = attr; 83 } 84 } 85 IsFull()86 inline bool IsFull() 87 { 88 return attr_ != nullptr ? attr_->HasValue(ASTAttr::FULL) : false; 89 } 90 IsLite()91 inline bool IsLite() 92 { 93 return attr_ != nullptr ? attr_->HasValue(ASTAttr::LITE) : false; 94 } 95 96 void SetBaseType(const AutoPtr<ASTType> &baseType); 97 98 AutoPtr<ASTType> GetBaseType(); 99 100 bool AddMember(const AutoPtr<ASTEnumValue> &member); 101 GetMembers()102 inline std::vector<AutoPtr<ASTEnumValue>> GetMembers() 103 { 104 return members_; 105 } 106 GetMemberNumber()107 inline size_t GetMemberNumber() 108 { 109 return members_.size(); 110 } 111 GetMember(size_t index)112 inline AutoPtr<ASTEnumValue> GetMember(size_t index) 113 { 114 if (index >= members_.size()) { 115 return nullptr; 116 } 117 return members_[index]; 118 } 119 HasMember(std::string memberName)120 inline bool HasMember(std::string memberName) 121 { 122 for (size_t i = 0; i < members_.size(); i++) { 123 if (members_[i]->GetName() == memberName) { 124 return true; 125 } 126 } 127 return false; 128 } 129 std::string GetSignature() override; 130 131 bool IsEnumType() override; 132 133 std::string Dump(const std::string &prefix) override; 134 135 TypeKind GetTypeKind() override; 136 private: 137 AutoPtr<ASTAttr> attr_ = new ASTAttr(); 138 AutoPtr<ASTType> baseType_; 139 AutoPtr<ASTType> parentType_; 140 std::vector<AutoPtr<ASTEnumValue>> members_; 141 }; 142 } // namespace Idl 143 } // namespace OHOS 144 145 #endif // OHOS_IDL_ASTENUMTYPE_H