1 /* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef OHOS_IDL_AST_H 17 #define OHOS_IDL_AST_H 18 19 #include <unordered_map> 20 #include <vector> 21 22 #include "ast/ast_void_type.h" 23 #include "ast/ast_array_type.h" 24 #include "ast/base/ast_boolean_type.h" 25 #include "ast/ast_native_buffer_type.h" 26 #include "ast/base/ast_byte_type.h" 27 #include "ast/base/ast_double_type.h" 28 #include "ast/ast_enum_type.h" 29 #include "ast/ast_fd_type.h" 30 #include "ast/base/ast_float_type.h" 31 #include "ast/base/ast_integer_type.h" 32 #include "ast/ast_interface_type.h" 33 #include "ast/base/ast_long_type.h" 34 #include "ast/ast_map_type.h" 35 #include "ast/ast_namespace.h" 36 #include "ast/ast_node.h" 37 #include "ast/ast_pointer_type.h" 38 #include "ast/ast_sequenceable_type.h" 39 #include "ast/base/ast_short_type.h" 40 #include "ast/ast_smq_type.h" 41 #include "ast/base/ast_string_type.h" 42 #include "ast/base/ast_string16_type.h" 43 #include "ast/ast_struct_type.h" 44 #include "ast/base/ast_char_type.h" 45 #include "ast/base/ast_uchar_type.h" 46 #include "ast/base/ast_uint_type.h" 47 #include "ast/base/ast_ulong_type.h" 48 #include "ast/ast_union_type.h" 49 #include "ast/base/ast_ushort_type.h" 50 #include "util/autoptr.h" 51 52 namespace OHOS { 53 namespace Idl { 54 enum class ASTFileType { 55 AST_IFACE, // this idl file contains class of normal interface 56 AST_CALL_IFACE, // this idl file contains class of interface that as parameter 57 AST_ICALLBACK, // this idl file contains class of callback interface 58 AST_TYPES, // this idl file contains custom types 59 AST_SEQUENCEABLE, // this is not an idl file, but a c++/java file 60 }; 61 62 class AST : public ASTNode { 63 public: 64 using StrASTMap = std::unordered_map<std::string, AutoPtr<AST>>; 65 using TypeStringMap = std::unordered_map<std::string, AutoPtr<ASTType>>; 66 67 ~AST() override = default; 68 SetAStFileType(ASTFileType fileType)69 void SetAStFileType(ASTFileType fileType) 70 { 71 astFileType_ = fileType; 72 } 73 GetASTFileType()74 ASTFileType GetASTFileType() const 75 { 76 return astFileType_; 77 } 78 79 void SetIdlFile(const std::string &idlFile); 80 GetName()81 inline std::string GetName() 82 { 83 return name_; 84 } 85 86 void SetFullName(const std::string &fullName); 87 GetFullName()88 inline std::string GetFullName() 89 { 90 return packageName_ + "." + name_; 91 } 92 GetPackageName()93 inline std::string GetPackageName() const 94 { 95 return packageName_; 96 } 97 SetLicense(const std::string & license)98 inline void SetLicense(const std::string &license) 99 { 100 license_ = license; 101 } 102 GetLicense()103 inline std::string GetLicense() 104 { 105 return license_; 106 } 107 108 void SetPackageName(const std::string &packageName); 109 110 AutoPtr<ASTNamespace> ParseNamespace(const std::string &nspaceStr); 111 112 void AddNamespace(const AutoPtr<ASTNamespace> &nspace); 113 114 AutoPtr<ASTNamespace> FindNamespace(const std::string &nspaceStr); 115 116 AutoPtr<ASTNamespace> GetNamespace(size_t index); 117 GetNamespace()118 inline std::vector<AutoPtr<ASTNamespace>> GetNamespace() 119 { 120 return namespaces_; 121 } 122 GetNamespaceNumber()123 inline size_t GetNamespaceNumber() 124 { 125 return namespaces_.size(); 126 } 127 128 void AddInterfaceDef(const AutoPtr<ASTInterfaceType> &interface); 129 130 AutoPtr<ASTInterfaceType> GetInterfaceDef(size_t index = 0); 131 GetInterfaceDefNumber()132 inline size_t GetInterfaceDefNumber() const 133 { 134 return interfaceDefs_.size(); 135 } 136 137 void AddSequenceableDef(const AutoPtr<ASTSequenceableType> &sequenceable); 138 139 AutoPtr<ASTSequenceableType> GetSequenceableDef(size_t index = 0); 140 GetSequenceableDefNumber()141 inline size_t GetSequenceableDefNumber() const 142 { 143 return sequenceableDefs_.size(); 144 } 145 146 int IndexOf(ASTInterfaceType* interface); 147 148 int IndexOf(ASTSequenceableType* sequenceable); 149 150 int IndexOf(ASTType* type); 151 152 void AddType(const AutoPtr<ASTType> &type); 153 154 AutoPtr<ASTType> FindType(const std::string &typeName, bool lookImports = true); 155 GetTypes()156 inline const TypeStringMap &GetTypes() const 157 { 158 return types_; 159 } 160 GetTypeNumber()161 inline size_t GetTypeNumber() const 162 { 163 return types_.size(); 164 } 165 166 void AddTypeDefinition(const AutoPtr<ASTType> &type); 167 GetTypeDefinitionNumber()168 inline size_t GetTypeDefinitionNumber() const 169 { 170 return typeDefinitions_.size(); 171 } 172 173 AutoPtr<ASTType> GetTypeDefintion(size_t index); 174 175 std::string Dump(const std::string &prefix) override; 176 177 bool AddImport(const AutoPtr<AST> &importAst); 178 ClearImport()179 void ClearImport() 180 { 181 return imports_.clear(); 182 } 183 GetImports()184 inline const StrASTMap &GetImports() const 185 { 186 return imports_; 187 } 188 189 void SetVersion(size_t &majorVer, size_t &minorVer); 190 GetMajorVer()191 inline size_t GetMajorVer() const 192 { 193 return majorVersion_; 194 } 195 GetMinorVer()196 inline size_t GetMinorVer() const 197 { 198 return minorVersion_; 199 } 200 GetVersion()201 std::string GetVersion() const 202 { 203 return StringHelper::Format("%u.%u", majorVersion_, minorVersion_); 204 } 205 GetIdlFilePath()206 inline std::string GetIdlFilePath() 207 { 208 return idlFilePath_; 209 } 210 211 bool IsValid(); GetInterfaceDefs()212 std::vector<AutoPtr<ASTInterfaceType>> GetInterfaceDefs() const 213 { 214 return interfaceDefs_; 215 } 216 GetHasCacheableProxyMethods()217 bool GetHasCacheableProxyMethods() const 218 { 219 return hasCacheableProxyMethods_; 220 } 221 SetHasCacheableProxyMethods(bool cacheable)222 void SetHasCacheableProxyMethods(bool cacheable) 223 { 224 hasCacheableProxyMethods_ = cacheable; 225 } 226 SetInterfaceToken(std::string & interfaceToken)227 inline void SetInterfaceToken(std::string &interfaceToken) 228 { 229 interfaceToken_ = interfaceToken; 230 } 231 GetInterfaceToken()232 inline std::string GetInterfaceToken() const 233 { 234 return interfaceToken_; 235 } 236 SetSupportDelegator(std::string & supportDelegator)237 inline void SetSupportDelegator(std::string &supportDelegator) 238 { 239 if (supportDelegator == "on") { 240 supportDelegatorOn_ = true; 241 } else { 242 supportDelegatorOn_ = false; 243 } 244 } 245 GetSupportDelegatorOn()246 inline bool GetSupportDelegatorOn() const 247 { 248 return supportDelegatorOn_; 249 } 250 251 private: 252 AutoPtr<ASTNamespace> NewNameSpace(std::string nameSpace); 253 254 ASTFileType astFileType_ = ASTFileType::AST_IFACE; 255 std::string name_; 256 std::string license_; 257 std::string packageName_; 258 size_t majorVersion_; 259 size_t minorVersion_; 260 std::vector<AutoPtr<ASTNamespace>> namespaces_; 261 std::vector<AutoPtr<ASTType>> typeDefinitions_; // enum, struct, union 262 std::vector<AutoPtr<ASTSequenceableType>> sequenceableDefs_; 263 std::vector<AutoPtr<ASTInterfaceType>> interfaceDefs_; 264 265 StrASTMap imports_; 266 TypeStringMap types_; 267 268 static TypeStringMap basicTypes_; 269 270 std::string idlFilePath_; 271 bool hasCacheableProxyMethods_ = false; 272 std::string interfaceToken_; 273 bool supportDelegatorOn_ = false; 274 }; 275 } // namespace Idl 276 } // namespace OHOS 277 278 #endif // OHOS_IDL_AST_H