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 SetParentType(const AutoPtr<ASTStructType> &parentType); 53 54 void AddMember(const AutoPtr<ASTType> &typeName, std::string name); 55 GetMembers()56 inline std::vector<std::tuple<std::string, AutoPtr<ASTType>>> GetMembers() 57 { 58 return members_; 59 } 60 GetMemberNumber()61 inline size_t GetMemberNumber() 62 { 63 return members_.size(); 64 } 65 GetMemberName(size_t index)66 inline std::string GetMemberName(size_t index) 67 { 68 if (index >= members_.size()) { 69 return std::string(""); 70 } 71 return std::get<0>(members_[index]); 72 } 73 GetMemberType(size_t index)74 inline AutoPtr<ASTType> GetMemberType(size_t index) 75 { 76 if (index >= members_.size()) { 77 return nullptr; 78 } 79 return std::get<1>(members_[index]); 80 } 81 82 bool IsStructType() override; 83 84 std::string Dump(const std::string &prefix) override; 85 86 TypeKind GetTypeKind() override; 87 88 std::string EmitCType(TypeMode mode = TypeMode::NO_MODE) const override; 89 90 std::string EmitCppType(TypeMode mode = TypeMode::NO_MODE) const override; 91 92 std::string EmitJavaType(TypeMode mode, bool isInnerType = false) const override; 93 94 std::string EmitCTypeDecl() const; 95 96 std::string EmitCppTypeDecl() const; 97 98 std::string EmitJavaTypeDecl() const; 99 100 void EmitCWriteVar(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 EmitCProxyReadVar(const std::string &parcelName, const std::string &name, bool isInnerType, 104 const std::string &ecName, const std::string &gotoLabel, StringBuilder &sb, 105 const std::string &prefix) const override; 106 107 void EmitCStubReadVar(const std::string &parcelName, const std::string &name, const std::string &ecName, 108 const std::string &gotoLabel, StringBuilder &sb, const std::string &prefix) const override; 109 110 void EmitCppWriteVar(const std::string &parcelName, const std::string &name, StringBuilder &sb, 111 const std::string &prefix, unsigned int innerLevel = 0) const override; 112 113 void EmitCppReadVar(const std::string &parcelName, const std::string &name, StringBuilder &sb, 114 const std::string &prefix, bool initVariable, unsigned int innerLevel = 0) const override; 115 116 void EmitCMarshalling(const std::string &name, StringBuilder &sb, const std::string &prefix) const override; 117 118 void EmitCUnMarshalling(const std::string &name, const std::string &gotoLabel, StringBuilder &sb, 119 const std::string &prefix, std::vector<std::string> &freeObjStatements) const override; 120 121 void EmitCppMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb, 122 const std::string &prefix, unsigned int innerLevel = 0) const override; 123 124 void EmitCppUnMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb, 125 const std::string &prefix, bool emitType, unsigned int innerLevel = 0) const override; 126 127 void EmitMemoryRecycle(const std::string &name, bool ownership, StringBuilder &sb, 128 const std::string &prefix) const override; 129 130 private: 131 AutoPtr<ASTAttr> attr_; 132 std::vector<std::tuple<std::string, AutoPtr<ASTType>>> members_; 133 AutoPtr<ASTStructType> parentType_; // used to dump parent type when using struct extension identify in idl 134 }; 135 } // namespace HDI 136 } // namespace OHOS 137 138 #endif // OHOS_HDI_ASTSTRUCTTYPE_H