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_ASTINTERFACETYPE_H 10 #define OHOS_HDI_ASTINTERFACETYPE_H 11 12 #include "ast/ast_attribute.h" 13 #include "ast/ast_method.h" 14 15 #include <vector> 16 17 #include "ast/ast_type.h" 18 #include "util/autoptr.h" 19 20 namespace OHOS { 21 namespace HDI { 22 class ASTInterfaceType : public ASTType { 23 public: ASTInterfaceType()24 ASTInterfaceType() 25 : ASTType(TypeKind::TYPE_INTERFACE, false), 26 license_(), 27 attr_(new ASTAttr()), 28 isSerializable_(false), 29 methods_(), 30 getVerMethod_(), 31 extendsInterface_(nullptr), 32 majorVersion_(1), 33 minorVersion_(0) 34 { 35 } 36 37 void SetNamespace(const AutoPtr<ASTNamespace> &nspace) override; 38 SetLicense(const std::string & license)39 inline void SetLicense(const std::string &license) 40 { 41 license_ = license; 42 } 43 GetLicense()44 inline std::string GetLicense() const 45 { 46 return license_; 47 } 48 SetAttribute(const AutoPtr<ASTAttr> & attr)49 void SetAttribute(const AutoPtr<ASTAttr> &attr) 50 { 51 if (attr != nullptr) { 52 attr_ = attr; 53 if (attr_->HasValue(ASTAttr::CALLBACK)) { 54 isSerializable_ = true; 55 } 56 } 57 } 58 GetAttribute()59 inline AutoPtr<ASTAttr> GetAttribute() const 60 { 61 return attr_; 62 } 63 IsOneWay()64 inline bool IsOneWay() const 65 { 66 return attr_->HasValue(ASTAttr::ONEWAY); 67 } 68 IsCallback()69 inline bool IsCallback() const 70 { 71 return attr_->HasValue(ASTAttr::CALLBACK); 72 } 73 SetSerializable(bool isSerializable)74 inline void SetSerializable(bool isSerializable) 75 { 76 isSerializable_ = isSerializable; 77 } 78 IsSerializable()79 inline bool IsSerializable() const 80 { 81 return isSerializable_; 82 } 83 IsFull()84 inline bool IsFull() const 85 { 86 return attr_->HasValue(ASTAttr::FULL); 87 } 88 IsLite()89 inline bool IsLite() const 90 { 91 return attr_->HasValue(ASTAttr::LITE); 92 } 93 IsMini()94 inline bool IsMini() const 95 { 96 return attr_->HasValue(ASTAttr::MINI); 97 } 98 99 void AddMethod(const AutoPtr<ASTMethod> &method); 100 101 AutoPtr<ASTMethod> GetMethod(size_t index); 102 103 std::vector<AutoPtr<ASTMethod>> GetMethodsBySystem(SystemLevel system) const; 104 GetMethodNumber()105 inline size_t GetMethodNumber() const 106 { 107 return methods_.size(); 108 } 109 AddVersionMethod(const AutoPtr<ASTMethod> & method)110 void AddVersionMethod(const AutoPtr<ASTMethod> &method) 111 { 112 getVerMethod_ = method; 113 } 114 GetVersionMethod()115 AutoPtr<ASTMethod> GetVersionMethod() 116 { 117 return getVerMethod_; 118 } 119 120 bool AddExtendsInterface(AutoPtr<ASTInterfaceType> interface); 121 GetExtendsInterface()122 AutoPtr<ASTInterfaceType> GetExtendsInterface() 123 { 124 return extendsInterface_; 125 } 126 GetMajorVersion()127 inline size_t GetMajorVersion() 128 { 129 return majorVersion_; 130 } 131 GetMinorVersion()132 inline size_t GetMinorVersion() 133 { 134 return minorVersion_; 135 } 136 137 void SetVersion(size_t &majorVer, size_t &minorVer); 138 139 bool IsInterfaceType() override; 140 141 std::string Dump(const std::string &prefix) override; 142 143 TypeKind GetTypeKind() override; 144 145 std::string GetFullName() const; 146 147 std::string EmitDescMacroName() const; 148 149 std::string EmitCType(TypeMode mode = TypeMode::NO_MODE) const override; 150 151 std::string EmitCppType(TypeMode mode = TypeMode::NO_MODE) const override; 152 153 std::string EmitJavaType(TypeMode mode, bool isInnerType = false) const override; 154 155 void EmitCWriteVar(const std::string &parcelName, const std::string &name, const std::string &ecName, 156 const std::string &gotoLabel, StringBuilder &sb, const std::string &prefix) const override; 157 158 void EmitCProxyReadVar(const std::string &parcelName, const std::string &name, bool isInnerType, 159 const std::string &ecName, const std::string &gotoLabel, StringBuilder &sb, 160 const std::string &prefix) const override; 161 162 void EmitCStubReadVar(const std::string &parcelName, const std::string &name, const std::string &ecName, 163 const std::string &gotoLabel, StringBuilder &sb, const std::string &prefix) const override; 164 165 void EmitCppWriteVar(const std::string &parcelName, const std::string &name, StringBuilder &sb, 166 const std::string &prefix, unsigned int innerLevel = 0) const override; 167 168 void EmitCppReadVar(const std::string &parcelName, const std::string &name, StringBuilder &sb, 169 const std::string &prefix, bool initVariable, unsigned int innerLevel = 0) const override; 170 171 void EmitJavaWriteVar(const std::string &parcelName, const std::string &name, StringBuilder &sb, 172 const std::string &prefix) const override; 173 174 void EmitJavaReadVar(const std::string &parcelName, const std::string &name, StringBuilder &sb, 175 const std::string &prefix) const override; 176 177 void EmitJavaReadInnerVar(const std::string &parcelName, const std::string &name, bool isInner, StringBuilder &sb, 178 const std::string &prefix) const override; 179 180 void RegisterWriteMethod(Language language, SerMode mode, UtilMethodMap &methods) const override; 181 182 void RegisterReadMethod(Language language, SerMode mode, UtilMethodMap &methods) const override; 183 184 void EmitCWriteMethods( 185 StringBuilder &sb, const std::string &prefix, const std::string &methodPrefix, bool isDecl) const; 186 187 void EmitCReadMethods( 188 StringBuilder &sb, const std::string &prefix, const std::string &methodPrefix, bool isDecl) const; 189 190 void EmitCppReadMethods( 191 StringBuilder &sb, const std::string &prefix, const std::string &methodPrefix, bool isDecl) const; 192 private: 193 std::string license_; 194 195 AutoPtr<ASTAttr> attr_; 196 bool isSerializable_; 197 std::vector<AutoPtr<ASTMethod>> methods_; 198 AutoPtr<ASTMethod> getVerMethod_; 199 AutoPtr<ASTInterfaceType> extendsInterface_; 200 size_t majorVersion_; 201 size_t minorVersion_; 202 }; 203 } // namespace HDI 204 } // namespace OHOS 205 206 #endif // OHOS_HDI_ASTINTERFACETYPE_H