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 GetBaseType()91 inline AutoPtr<ASTType> GetBaseType() 92 { 93 return baseType_; 94 } 95 96 void AddMember(const AutoPtr<ASTEnumValue> &member); 97 GetMemberNumber()98 inline size_t GetMemberNumber() 99 { 100 return members_.size(); 101 } 102 GetMember(size_t index)103 inline AutoPtr<ASTEnumValue> GetMember(size_t index) 104 { 105 if (index >= members_.size()) { 106 return nullptr; 107 } 108 return members_[index]; 109 } 110 111 bool IsEnumType() override; 112 113 std::string Dump(const std::string &prefix) override; 114 115 TypeKind GetTypeKind() override; 116 117 std::string EmitCType(TypeMode mode = TypeMode::NO_MODE) const override; 118 119 std::string EmitCppType(TypeMode mode = TypeMode::NO_MODE) const override; 120 121 std::string EmitJavaType(TypeMode mode, bool isInnerType = false) const override; 122 123 std::string EmitCTypeDecl() const; 124 125 std::string EmitCppTypeDecl() const; 126 127 std::string EmitJavaTypeDecl() const; 128 129 void EmitCWriteVar(const std::string &parcelName, const std::string &name, const std::string &ecName, 130 const std::string &gotoLabel, StringBuilder &sb, const std::string &prefix) const override; 131 132 void EmitCProxyReadVar(const std::string &parcelName, const std::string &name, bool isInnerType, 133 const std::string &ecName, const std::string &gotoLabel, StringBuilder &sb, 134 const std::string &prefix) const override; 135 136 void EmitCStubReadVar(const std::string &parcelName, const std::string &name, const std::string &ecName, 137 const std::string &gotoLabel, StringBuilder &sb, const std::string &prefix) const override; 138 139 void EmitCppWriteVar(const std::string &parcelName, const std::string &name, StringBuilder &sb, 140 const std::string &prefix, unsigned int innerLevel = 0) const override; 141 142 void EmitCppReadVar(const std::string &parcelName, const std::string &name, StringBuilder &sb, 143 const std::string &prefix, bool initVariable, unsigned int innerLevel = 0) const override; 144 145 void EmitCMarshalling(const std::string &name, StringBuilder &sb, const std::string &prefix) const override; 146 147 void EmitCUnMarshalling(const std::string &name, const std::string &gotoLabel, StringBuilder &sb, 148 const std::string &prefix, std::vector<std::string> &freeObjStatements) const override; 149 150 void EmitCppMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb, 151 const std::string &prefix, unsigned int innerLevel = 0) const override; 152 153 void EmitCppUnMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb, 154 const std::string &prefix, bool emitType, unsigned int innerLevel = 0) const override; 155 156 private: 157 AutoPtr<ASTAttr> attr_ = new ASTAttr(); 158 AutoPtr<ASTType> baseType_; 159 std::vector<AutoPtr<ASTEnumValue>> members_; 160 }; 161 } // namespace HDI 162 } // namespace OHOS 163 164 #endif // OHOS_HDI_ASTENUMTYPE_H