1 /* 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * 4 * HDF is dual licensed: you can use it either under the terms of 5 * the GPL, or the BSD license, at your option. 6 * See the LICENSE file in the root of this repository for complete details. 7 */ 8 9 #ifndef OHOS_HDI_ASTENUMTYPE_H 10 #define OHOS_HDI_ASTENUMTYPE_H 11 12 #include "ast/ast_attribute.h" 13 #include "ast/ast_expr.h" 14 #include "ast/ast_type.h" 15 #include "util/autoptr.h" 16 17 #include <vector> 18 19 namespace OHOS { 20 namespace HDI { 21 class ASTEnumValue : public ASTNode { 22 public: ASTEnumValue(const std::string & name)23 explicit ASTEnumValue(const std::string &name) : mName_(name), value_(nullptr) {} 24 ~ASTEnumValue()25 inline ~ASTEnumValue() override {} 26 GetName()27 inline std::string GetName() 28 { 29 return mName_; 30 } 31 SetType(const AutoPtr<ASTType> & type)32 inline void SetType(const AutoPtr<ASTType> &type) 33 { 34 mType_ = type; 35 } 36 GetType()37 inline AutoPtr<ASTType> GetType() 38 { 39 return mType_; 40 } 41 SetExprValue(const AutoPtr<ASTExpr> & value)42 inline void SetExprValue(const AutoPtr<ASTExpr> &value) 43 { 44 value_ = value; 45 } 46 GetExprValue()47 inline AutoPtr<ASTExpr> GetExprValue() 48 { 49 return value_; 50 } 51 52 private: 53 std::string mName_; 54 AutoPtr<ASTType> mType_; 55 AutoPtr<ASTExpr> value_; 56 }; 57 58 class ASTEnumType : public ASTType { 59 public: ASTEnumType()60 ASTEnumType() : ASTType(TypeKind::TYPE_ENUM, true), attr_(new ASTAttr()), baseType_(), members_() {} 61 SetName(const std::string & name)62 inline void SetName(const std::string &name) override 63 { 64 name_ = name; 65 } 66 GetName()67 inline std::string GetName() override 68 { 69 return name_; 70 } 71 SetAttribute(const AutoPtr<ASTAttr> & attr)72 inline void SetAttribute(const AutoPtr<ASTAttr> &attr) 73 { 74 if (attr != nullptr) { 75 attr_ = attr; 76 } 77 } 78 IsFull()79 inline bool IsFull() 80 { 81 return attr_ != nullptr ? attr_->HasValue(ASTAttr::FULL) : false; 82 } 83 IsLite()84 inline bool IsLite() 85 { 86 return attr_ != nullptr ? attr_->HasValue(ASTAttr::LITE) : false; 87 } 88 89 void SetBaseType(const AutoPtr<ASTType> &baseType); 90 91 AutoPtr<ASTType> GetBaseType(); 92 93 bool AddMember(const AutoPtr<ASTEnumValue> &member); 94 GetMembers()95 inline std::vector<AutoPtr<ASTEnumValue>> GetMembers() 96 { 97 return members_; 98 } 99 GetMemberNumber()100 inline size_t GetMemberNumber() 101 { 102 return members_.size(); 103 } 104 GetMember(size_t index)105 inline AutoPtr<ASTEnumValue> GetMember(size_t index) 106 { 107 if (index >= members_.size()) { 108 return nullptr; 109 } 110 return members_[index]; 111 } 112 HasMember(const std::string & memberName)113 inline bool HasMember(const std::string &memberName) 114 { 115 for (size_t i = 0; i < members_.size(); i++) { 116 if (members_[i]->GetName() == memberName) { 117 return true; 118 } 119 } 120 return false; 121 } 122 123 bool IsEnumType() override; 124 125 std::string Dump(const std::string &prefix) override; 126 127 TypeKind GetTypeKind() override; 128 129 std::string EmitCType(TypeMode mode = TypeMode::NO_MODE) const override; 130 131 std::string EmitCppType(TypeMode mode = TypeMode::NO_MODE) const override; 132 133 std::string EmitJavaType(TypeMode mode, bool isInnerType = false) const override; 134 135 std::string EmitCTypeDecl() const; 136 137 std::string EmitCppTypeDecl() const; 138 139 std::string EmitJavaTypeDecl() const; 140 141 void EmitCWriteVar(const std::string &parcelName, const std::string &name, const std::string &ecName, 142 const std::string &gotoLabel, StringBuilder &sb, const std::string &prefix) const override; 143 144 void EmitCProxyReadVar(const std::string &parcelName, const std::string &name, bool isInnerType, 145 const std::string &ecName, const std::string &gotoLabel, StringBuilder &sb, 146 const std::string &prefix) const override; 147 148 void EmitCStubReadVar(const std::string &parcelName, const std::string &name, const std::string &ecName, 149 const std::string &gotoLabel, StringBuilder &sb, const std::string &prefix) const override; 150 151 void EmitCppWriteVar(const std::string &parcelName, const std::string &name, StringBuilder &sb, 152 const std::string &prefix, unsigned int innerLevel = 0) const override; 153 154 void EmitCppReadVar(const std::string &parcelName, const std::string &name, StringBuilder &sb, 155 const std::string &prefix, bool initVariable, unsigned int innerLevel = 0) const override; 156 157 void EmitCMarshalling(const std::string &name, StringBuilder &sb, const std::string &prefix) const override; 158 159 void EmitCUnMarshalling(const std::string &name, const std::string &gotoLabel, StringBuilder &sb, 160 const std::string &prefix, std::vector<std::string> &freeObjStatements) const override; 161 162 void EmitCppMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb, 163 const std::string &prefix, unsigned int innerLevel = 0) const override; 164 165 void EmitCppUnMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb, 166 const std::string &prefix, bool emitType, unsigned int innerLevel = 0) const override; 167 168 private: 169 AutoPtr<ASTAttr> attr_ = new ASTAttr(); 170 AutoPtr<ASTType> baseType_; 171 AutoPtr<ASTType> parentType_; 172 std::vector<AutoPtr<ASTEnumValue>> members_; 173 }; 174 } // namespace HDI 175 } // namespace OHOS 176 177 #endif // OHOS_HDI_ASTENUMTYPE_H