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_ASTSTRUCTTYPE_H 10 #define OHOS_HDI_ASTSTRUCTTYPE_H 11 12 #include <tuple> 13 #include <vector> 14 15 #include "ast/ast_attribute.h" 16 #include "ast/ast_type.h" 17 #include "util/autoptr.h" 18 19 namespace OHOS { 20 namespace HDI { 21 class ASTStructType : public ASTType { 22 public: ASTStructType()23 ASTStructType() : ASTType(TypeKind::TYPE_STRUCT, true), attr_(new ASTAttr()), members_() {} 24 SetName(const std::string & name)25 inline void SetName(const std::string &name) override 26 { 27 name_ = name; 28 } 29 GetName()30 inline std::string GetName() override 31 { 32 return name_; 33 } 34 SetAttribute(const AutoPtr<ASTAttr> & attr)35 inline void SetAttribute(const AutoPtr<ASTAttr> &attr) 36 { 37 if (attr != nullptr) { 38 attr_ = attr; 39 } 40 } 41 IsFull()42 inline bool IsFull() 43 { 44 return attr_ != nullptr ? attr_->HasValue(ASTAttr::FULL) : false; 45 } 46 IsLite()47 inline bool IsLite() 48 { 49 return attr_ != nullptr ? attr_->HasValue(ASTAttr::LITE) : false; 50 } 51 52 void AddMember(const AutoPtr<ASTType> &typeName, std::string name); 53 GetMemberNumber()54 inline size_t GetMemberNumber() 55 { 56 return members_.size(); 57 } 58 GetMemberName(size_t index)59 inline std::string GetMemberName(size_t index) 60 { 61 if (index >= members_.size()) { 62 return std::string(""); 63 } 64 return std::get<0>(members_[index]); 65 } 66 GetMemberType(size_t index)67 inline AutoPtr<ASTType> GetMemberType(size_t index) 68 { 69 if (index >= members_.size()) { 70 return nullptr; 71 } 72 return std::get<1>(members_[index]); 73 } 74 75 bool IsStructType() override; 76 77 std::string Dump(const std::string &prefix) override; 78 79 TypeKind GetTypeKind() override; 80 81 std::string EmitCType(TypeMode mode = TypeMode::NO_MODE) const override; 82 83 std::string EmitCppType(TypeMode mode = TypeMode::NO_MODE) const override; 84 85 std::string EmitJavaType(TypeMode mode, bool isInnerType = false) const override; 86 87 std::string EmitCTypeDecl() const; 88 89 std::string EmitCppTypeDecl() const; 90 91 std::string EmitJavaTypeDecl() const; 92 93 void EmitCWriteVar(const std::string &parcelName, const std::string &name, const std::string &ecName, 94 const std::string &gotoLabel, StringBuilder &sb, const std::string &prefix) const override; 95 96 void EmitCProxyReadVar(const std::string &parcelName, const std::string &name, bool isInnerType, 97 const std::string &ecName, const std::string &gotoLabel, StringBuilder &sb, 98 const std::string &prefix) const override; 99 100 void EmitCStubReadVar(const std::string &parcelName, const std::string &name, const std::string &ecName, 101 const std::string &gotoLabel, StringBuilder &sb, const std::string &prefix) const override; 102 103 void EmitCppWriteVar(const std::string &parcelName, const std::string &name, StringBuilder &sb, 104 const std::string &prefix, unsigned int innerLevel = 0) const override; 105 106 void EmitCppReadVar(const std::string &parcelName, const std::string &name, StringBuilder &sb, 107 const std::string &prefix, bool initVariable, unsigned int innerLevel = 0) const override; 108 109 void EmitCMarshalling(const std::string &name, StringBuilder &sb, const std::string &prefix) const override; 110 111 void EmitCUnMarshalling(const std::string &name, const std::string &gotoLabel, StringBuilder &sb, 112 const std::string &prefix, std::vector<std::string> &freeObjStatements) const override; 113 114 void EmitCppMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb, 115 const std::string &prefix, unsigned int innerLevel = 0) const override; 116 117 void EmitCppUnMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb, 118 const std::string &prefix, bool emitType, unsigned int innerLevel = 0) const override; 119 120 void EmitMemoryRecycle(const std::string &name, bool isClient, bool ownership, StringBuilder &sb, 121 const std::string &prefix) const override; 122 123 private: 124 AutoPtr<ASTAttr> attr_; 125 std::vector<std::tuple<std::string, AutoPtr<ASTType>>> members_; 126 }; 127 } // namespace HDI 128 } // namespace OHOS 129 130 #endif // OHOS_HDI_ASTSTRUCTTYPE_H