1 /* 2 * Copyright (c) 2021 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 "ast/ast_namespace.h" 13 #include "ast/ast_node.h" 14 #include "util/autoptr.h" 15 #include "util/string_builder.h" 16 17 namespace OHOS { 18 namespace HDI { 19 extern const char* g_tab; 20 21 enum class TypeKind { 22 TYPE_UNKNOWN = 0, 23 TYPE_BOOLEAN, 24 TYPE_BYTE, 25 TYPE_SHORT, 26 TYPE_INT, 27 TYPE_LONG, 28 TYPE_FLOAT, 29 TYPE_DOUBLE, 30 TYPE_UCHAR, 31 TYPE_USHORT, 32 TYPE_UINT, 33 TYPE_ULONG, 34 TYPE_STRING, 35 TYPE_FILEDESCRIPTOR, 36 TYPE_VOID, 37 TYPE_SEQUENCEABLE, 38 TYPE_INTERFACE, 39 TYPE_LIST, 40 TYPE_MAP, 41 TYPE_ARRAY, 42 TYPE_ENUM, 43 TYPE_STRUCT, 44 TYPE_UNION, 45 TYPE_SMQ, 46 }; 47 48 enum class TypeMode { 49 NO_MODE, // only type 50 PARAM_IN, // type of the in attribute parameter 51 PARAM_OUT, // type of the out attribute parameter 52 LOCAL_VAR, // type of the local variable 53 }; 54 55 enum class LanguageType { 56 LANG_C, 57 LANG_CPP, 58 LANG_JAVA, 59 }; 60 61 class ASTType : public ASTNode { 62 public: 63 virtual void SetName(const String& name); 64 65 virtual String GetName(); 66 67 virtual void SetNamespace(const AutoPtr<ASTNamespace>& nspace); 68 69 virtual AutoPtr<ASTNamespace> GetNamespace(); 70 71 virtual bool IsBooleanType(); 72 73 virtual bool IsByteType(); 74 75 virtual bool IsShortType(); 76 77 virtual bool IsIntegerType(); 78 79 virtual bool IsLongType(); 80 81 virtual bool IsUcharType(); 82 83 virtual bool IsUshortType(); 84 85 virtual bool IsUintType(); 86 87 virtual bool IsUlongType(); 88 89 virtual bool IsFloatType(); 90 91 virtual bool IsDoubleType(); 92 93 virtual bool IsStringType(); 94 95 virtual bool IsListType(); 96 97 virtual bool IsMapType(); 98 99 virtual bool IsEnumType(); 100 101 virtual bool IsStructType(); 102 103 virtual bool IsUnionType(); 104 105 virtual bool IsInterfaceType(); 106 107 virtual bool IsSequenceableType(); 108 109 virtual bool IsVoidType(); 110 111 virtual bool IsArrayType(); 112 113 virtual bool IsFdType(); 114 115 virtual bool IsSmqType(); 116 117 virtual String ToShortString(); 118 119 String ToString() override; 120 121 virtual TypeKind GetTypeKind(); 122 123 virtual String EmitCType(TypeMode mode = TypeMode::NO_MODE) const; 124 125 virtual String EmitCppType(TypeMode mode = TypeMode::NO_MODE) const; 126 127 virtual String EmitJavaType(TypeMode mode, bool isInnerType = false) const; 128 129 virtual void EmitCWriteVar(const String& parcelName, const String& name, const String& ecName, 130 const String& gotoLabel, StringBuilder& sb, const String& prefix) const; 131 132 virtual void EmitCProxyWriteOutVar(const String& parcelName, const String& name, const String& ecName, 133 const String& gotoLabel, StringBuilder& sb, const String& prefix) const; 134 135 virtual void EmitCProxyReadVar(const String& parcelName, const String& name, bool isInnerType, 136 const String& ecName, const String& gotoLabel, StringBuilder& sb, const String& prefix) const; 137 138 virtual void EmitCStubReadVar(const String& parcelName, const String& name, const String& ecName, 139 const String& gotoLabel, StringBuilder& sb, const String& prefix) const; 140 141 virtual void EmitCStubReadOutVar(const String& parcelName, const String& name, const String& ecName, 142 const String& gotoLabel, StringBuilder& sb, const String& prefix) const; 143 144 virtual void EmitCppWriteVar(const String& parcelName, const String& name, StringBuilder& sb, 145 const String& prefix, unsigned int innerLevel = 0) const; 146 147 virtual void EmitCppReadVar(const String& parcelName, const String& name, StringBuilder& sb, 148 const String& prefix, bool initVariable, unsigned int innerLevel = 0) const; 149 150 virtual void EmitCMarshalling(const String& name, StringBuilder& sb, const String& prefix) const; 151 152 virtual void EmitCUnMarshalling(const String& name, const String& gotoLabel, StringBuilder& sb, 153 const String& prefix, std::vector<String>& freeObjStatements) const; 154 155 void EmitFreeStatements(const std::vector<String>& freeObjStatements, StringBuilder& sb, 156 const String& prefix) const; 157 158 virtual void EmitCppMarshalling(const String& parcelName, const String& name, StringBuilder& sb, 159 const String& prefix, unsigned int innerLevel = 0) const; 160 161 virtual void EmitCppUnMarshalling(const String& parcelName, const String& name, StringBuilder& sb, 162 const String& prefix, bool emitType, unsigned int innerLevel = 0) const; 163 164 virtual void EmitMemoryRecycle(const String& name, bool isClient, bool ownership, StringBuilder& sb, 165 const String& prefix) const; 166 167 virtual void EmitJavaWriteVar(const String& parcelName, const String& name, StringBuilder& sb, 168 const String& prefix) const; 169 170 virtual void EmitJavaReadVar(const String& parcelName, const String& name, StringBuilder& sb, 171 const String& prefix) const; 172 173 virtual void EmitJavaReadInnerVar(const String& parcelName, const String& name, bool isInner, StringBuilder& sb, 174 const String& prefix) const; 175 protected: 176 String name_; 177 AutoPtr<ASTNamespace> namespace_; 178 TypeKind typeKind_; 179 }; 180 } // namespace HDI 181 } // namespace OHOS 182 183 #endif // OHOS_HDI_ASTTYPE_H