1 /* 2 * Copyright (c) 2021-2023 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_ASTTYPE_H 10 #define OHOS_HDI_ASTTYPE_H 11 12 #include <functional> 13 #include <regex> 14 #include <unordered_map> 15 16 #include "ast/ast_namespace.h" 17 #include "ast/ast_node.h" 18 #include "util/autoptr.h" 19 #include "util/options.h" 20 #include "util/string_builder.h" 21 22 namespace OHOS { 23 namespace HDI { 24 enum class TypeKind { 25 TYPE_UNKNOWN = 0, 26 TYPE_BOOLEAN, 27 TYPE_BYTE, 28 TYPE_SHORT, 29 TYPE_INT, 30 TYPE_LONG, 31 TYPE_FLOAT, 32 TYPE_DOUBLE, 33 TYPE_UCHAR, 34 TYPE_USHORT, 35 TYPE_UINT, 36 TYPE_ULONG, 37 TYPE_STRING, 38 TYPE_FILEDESCRIPTOR, 39 TYPE_SEQUENCEABLE, 40 TYPE_INTERFACE, 41 TYPE_LIST, 42 TYPE_MAP, 43 TYPE_ARRAY, 44 TYPE_ENUM, 45 TYPE_STRUCT, 46 TYPE_UNION, 47 TYPE_SMQ, 48 TYPE_ASHMEM, 49 TYPE_NATIVE_BUFFER, 50 TYPE_POINTER, 51 }; 52 53 enum class TypeMode { 54 NO_MODE, // only type 55 PARAM_IN, // type of the in attribute parameter 56 PARAM_OUT, // type of the out attribute parameter 57 LOCAL_VAR, // type of the local variable 58 }; 59 60 enum class SerMode { 61 PROXY_SER, // the flag of proxy serialized 62 STUB_SER, // the flag of stub serialized 63 CUSTOM_SER, // the flag of custom types serialized 64 }; 65 66 using UtilMethod = std::function<void(StringBuilder &, const std::string &, const std::string &, bool)>; 67 using UtilMethodMap = std::unordered_map<std::string, UtilMethod>; 68 69 class ASTType : public ASTNode { 70 public: 71 explicit ASTType(TypeKind kind = TypeKind::TYPE_UNKNOWN, bool isPod = true) typeKind_(kind)72 :typeKind_(kind), isPod_(isPod), name_(), namespace_() 73 { 74 } 75 76 virtual void SetName(const std::string &name); 77 78 virtual std::string GetName(); 79 80 virtual void SetNamespace(const AutoPtr<ASTNamespace> &nspace); 81 82 virtual AutoPtr<ASTNamespace> GetNamespace(); 83 84 virtual bool IsBooleanType(); 85 86 virtual bool IsByteType(); 87 88 virtual bool IsShortType(); 89 90 virtual bool IsIntegerType(); 91 92 virtual bool IsLongType(); 93 94 virtual bool IsUcharType(); 95 96 virtual bool IsUshortType(); 97 98 virtual bool IsUintType(); 99 100 virtual bool IsUlongType(); 101 102 virtual bool IsFloatType(); 103 104 virtual bool IsDoubleType(); 105 106 virtual bool IsStringType(); 107 108 virtual bool IsListType(); 109 110 virtual bool IsMapType(); 111 112 virtual bool IsEnumType(); 113 114 virtual bool IsStructType(); 115 116 virtual bool IsUnionType(); 117 118 virtual bool IsInterfaceType(); 119 120 virtual bool IsSequenceableType(); 121 122 virtual bool IsArrayType(); 123 124 virtual bool IsFdType(); 125 126 virtual bool IsSmqType(); 127 128 virtual bool IsAshmemType(); 129 130 virtual bool IsNativeBufferType(); 131 132 virtual bool IsPointerType(); 133 134 bool IsPod() const; 135 136 virtual bool HasInnerType(TypeKind innerType) const; 137 138 virtual std::string ToShortString(); 139 140 std::string ToString() const override; 141 142 virtual TypeKind GetTypeKind(); 143 144 virtual std::string EmitCType(TypeMode mode = TypeMode::NO_MODE) const; 145 146 virtual std::string EmitCppType(TypeMode mode = TypeMode::NO_MODE) const; 147 148 virtual std::string EmitJavaType(TypeMode mode, bool isInnerType = false) const; 149 150 virtual void EmitCWriteVar(const std::string &parcelName, const std::string &name, const std::string &ecName, 151 const std::string &gotoLabel, StringBuilder &sb, const std::string &prefix) const; 152 153 virtual void EmitCProxyWriteOutVar(const std::string &parcelName, const std::string &name, 154 const std::string &ecName, const std::string &gotoLabel, StringBuilder &sb, const std::string &prefix) const; 155 156 virtual void EmitCProxyReadVar(const std::string &parcelName, const std::string &name, bool isInnerType, 157 const std::string &ecName, const std::string &gotoLabel, StringBuilder &sb, const std::string &prefix) const; 158 159 virtual void EmitCStubReadVar(const std::string &parcelName, const std::string &name, const std::string &ecName, 160 const std::string &gotoLabel, StringBuilder &sb, const std::string &prefix) const; 161 162 virtual void EmitCStubReadOutVar(const std::string &buffSizeName, const std::string &memFlagName, 163 const std::string &parcelName, const std::string &name, const std::string &ecName, const std::string &gotoLabel, 164 StringBuilder &sb, const std::string &prefix) const; 165 166 virtual void EmitCppWriteVar(const std::string &parcelName, const std::string &name, StringBuilder &sb, 167 const std::string &prefix, unsigned int innerLevel = 0) const; 168 169 virtual void EmitCppReadVar(const std::string &parcelName, const std::string &name, StringBuilder &sb, 170 const std::string &prefix, bool initVariable, unsigned int innerLevel = 0) const; 171 172 virtual void EmitCMarshalling(const std::string &name, StringBuilder &sb, const std::string &prefix) const; 173 174 virtual void EmitCUnMarshalling(const std::string &name, const std::string &gotoLabel, StringBuilder &sb, 175 const std::string &prefix, std::vector<std::string> &freeObjStatements) const; 176 177 void EmitFreeStatements( 178 const std::vector<std::string> &freeObjStatements, StringBuilder &sb, const std::string &prefix) const; 179 180 virtual void EmitCppMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb, 181 const std::string &prefix, unsigned int innerLevel = 0) const; 182 183 virtual void EmitCppUnMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb, 184 const std::string &prefix, bool emitType, unsigned int innerLevel = 0) const; 185 186 virtual void EmitMemoryRecycle( 187 const std::string &name, bool isClient, bool ownership, StringBuilder &sb, const std::string &prefix) const; 188 189 virtual void EmitJavaWriteVar( 190 const std::string &parcelName, const std::string &name, StringBuilder &sb, const std::string &prefix) const; 191 192 virtual void EmitJavaReadVar( 193 const std::string &parcelName, const std::string &name, StringBuilder &sb, const std::string &prefix) const; 194 195 virtual void EmitJavaReadInnerVar(const std::string &parcelName, const std::string &name, bool isInner, 196 StringBuilder &sb, const std::string &prefix) const; 197 198 virtual void RegisterWriteMethod(Language language, SerMode mode, UtilMethodMap &methods) const; 199 200 virtual void RegisterReadMethod(Language language, SerMode mode, UtilMethodMap &methods) const; 201 202 virtual std::string GetNameWithNamespace(AutoPtr<ASTNamespace> space, std::string name) const; 203 204 virtual std::string PascalName(const std::string &name) const; 205 206 protected: 207 TypeKind typeKind_; 208 bool isPod_; 209 std::string name_; 210 AutoPtr<ASTNamespace> namespace_; 211 }; 212 } // namespace HDI 213 } // namespace OHOS 214 215 #endif // OHOS_HDI_ASTTYPE_H