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 ASTInfAttr()), 28 isSerializable_(false), 29 methods_(), 30 getVerMethod_() 31 { 32 } 33 34 void SetNamespace(const AutoPtr<ASTNamespace> &nspace) override; 35 SetLicense(const std::string & license)36 inline void SetLicense(const std::string &license) 37 { 38 license_ = license; 39 } 40 GetLicense()41 inline std::string GetLicense() const 42 { 43 return license_; 44 } 45 SetAttribute(const AutoPtr<ASTInfAttr> & attr)46 void SetAttribute(const AutoPtr<ASTInfAttr> &attr) 47 { 48 if (attr != nullptr) { 49 attr_ = attr; 50 if (attr_->isCallback_) { 51 isSerializable_ = true; 52 } 53 } 54 } 55 IsOneWay()56 inline bool IsOneWay() 57 { 58 return attr_->isOneWay_; 59 } 60 IsCallback()61 inline bool IsCallback() 62 { 63 return attr_->isCallback_; 64 } 65 SetSerializable(bool isSerializable)66 inline void SetSerializable(bool isSerializable) 67 { 68 isSerializable_ = isSerializable; 69 } 70 IsSerializable()71 inline bool IsSerializable() 72 { 73 return isSerializable_; 74 } 75 IsFull()76 inline bool IsFull() 77 { 78 return attr_->isFull_; 79 } 80 IsLite()81 inline bool IsLite() 82 { 83 return attr_->isLite_; 84 } 85 86 void AddMethod(const AutoPtr<ASTMethod> &method); 87 88 AutoPtr<ASTMethod> GetMethod(size_t index); 89 GetMethodNumber()90 inline size_t GetMethodNumber() 91 { 92 return methods_.size(); 93 } 94 AddVersionMethod(const AutoPtr<ASTMethod> & method)95 void AddVersionMethod(const AutoPtr<ASTMethod> &method) 96 { 97 getVerMethod_ = method; 98 } 99 GetVersionMethod()100 AutoPtr<ASTMethod> GetVersionMethod() 101 { 102 return getVerMethod_; 103 } 104 105 bool IsInterfaceType() override; 106 107 std::string ToString() const override; 108 109 std::string Dump(const std::string &prefix) override; 110 111 TypeKind GetTypeKind() override; 112 113 std::string GetFullName() const; 114 115 std::string EmitDescMacroName() const; 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 void EmitCWriteVar(const std::string &parcelName, const std::string &name, const std::string &ecName, 124 const std::string &gotoLabel, StringBuilder &sb, const std::string &prefix) const override; 125 126 void EmitCProxyReadVar(const std::string &parcelName, const std::string &name, bool isInnerType, 127 const std::string &ecName, const std::string &gotoLabel, StringBuilder &sb, 128 const std::string &prefix) const override; 129 130 void EmitCStubReadVar(const std::string &parcelName, const std::string &name, const std::string &ecName, 131 const std::string &gotoLabel, StringBuilder &sb, const std::string &prefix) const override; 132 133 void EmitCppWriteVar(const std::string &parcelName, const std::string &name, StringBuilder &sb, 134 const std::string &prefix, unsigned int innerLevel = 0) const override; 135 136 void EmitCppReadVar(const std::string &parcelName, const std::string &name, StringBuilder &sb, 137 const std::string &prefix, bool initVariable, unsigned int innerLevel = 0) const override; 138 139 void EmitJavaWriteVar(const std::string &parcelName, const std::string &name, StringBuilder &sb, 140 const std::string &prefix) const override; 141 142 void EmitJavaReadVar(const std::string &parcelName, const std::string &name, StringBuilder &sb, 143 const std::string &prefix) const override; 144 145 void EmitJavaReadInnerVar(const std::string &parcelName, const std::string &name, bool isInner, StringBuilder &sb, 146 const std::string &prefix) const override; 147 148 void RegisterWriteMethod(Options::Language language, SerMode mode, UtilMethodMap &methods) const override; 149 150 void RegisterReadMethod(Options::Language language, SerMode mode, UtilMethodMap &methods) const override; 151 152 void EmitCWriteMethods( 153 StringBuilder &sb, const std::string &prefix, const std::string &methodPrefix, bool isDecl) const; 154 155 void EmitCReadMethods( 156 StringBuilder &sb, const std::string &prefix, const std::string &methodPrefix, bool isDecl) const; 157 158 void EmitCppReadMethods( 159 StringBuilder &sb, const std::string &prefix, const std::string &methodPrefix, bool isDecl) const; 160 private: 161 std::string license_; 162 163 AutoPtr<ASTInfAttr> attr_; 164 bool isSerializable_; 165 std::vector<AutoPtr<ASTMethod>> methods_; 166 AutoPtr<ASTMethod> getVerMethod_; 167 }; 168 } // namespace HDI 169 } // namespace OHOS 170 171 #endif // OHOS_HDI_ASTINTERFACETYPE_H