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_rawdata_type.h" 39 #include "ast/ast_sequenceable_type.h" 40 #include "ast/base/ast_short_type.h" 41 #include "ast/ast_smq_type.h" 42 #include "ast/base/ast_string_type.h" 43 #include "ast/base/ast_string16_type.h" 44 #include "ast/ast_struct_type.h" 45 #include "ast/base/ast_char_type.h" 46 #include "ast/base/ast_uchar_type.h" 47 #include "ast/base/ast_uint_type.h" 48 #include "ast/base/ast_ulong_type.h" 49 #include "ast/ast_union_type.h" 50 #include "ast/base/ast_ushort_type.h" 51 #include "util/autoptr.h" 52 53 namespace OHOS { 54 namespace Idl { 55 enum class ASTFileType { 56 AST_IFACE, // this idl file contains class of normal interface 57 AST_CALL_IFACE, // this idl file contains class of interface that as parameter 58 AST_ICALLBACK, // this idl file contains class of callback interface 59 AST_TYPES, // this idl file contains custom types 60 AST_SEQUENCEABLE, // this is not an idl file, but a c++/java file 61 AST_RAWDATA, // this is not an idl file, but a c++/java file 62 }; 63 64 class AST : public ASTNode { 65 public: 66 using StrASTMap = std::unordered_map<std::string, AutoPtr<AST>>; 67 using TypeStringMap = std::unordered_map<std::string, AutoPtr<ASTType>>; 68 69 ~AST() override = default; 70 SetAStFileType(ASTFileType fileType)71 void SetAStFileType(ASTFileType fileType) 72 { 73 astFileType_ = fileType; 74 } 75 GetASTFileType()76 ASTFileType GetASTFileType() const 77 { 78 return astFileType_; 79 } 80 81 void SetIdlFile(const std::string &idlFile); 82 GetName()83 inline std::string GetName() 84 { 85 return name_; 86 } 87 88 void SetFullName(const std::string &fullName); 89 GetFullName()90 inline std::string GetFullName() 91 { 92 return packageName_ + "." + name_; 93 } 94 GetPackageName()95 inline std::string GetPackageName() const 96 { 97 return packageName_; 98 } 99 SetLicense(const std::string & license)100 inline void SetLicense(const std::string &license) 101 { 102 license_ = license; 103 } 104 GetLicense()105 inline std::string GetLicense() 106 { 107 return license_; 108 } 109 GetIdlFile()110 inline std::string GetIdlFile() 111 { 112 return idlFilePath_; 113 } 114 115 void SetPackageName(const std::string &packageName); 116 117 AutoPtr<ASTNamespace> ParseNamespace(const std::string &nspaceStr); 118 119 void AddNamespace(const AutoPtr<ASTNamespace> &nspace); 120 121 AutoPtr<ASTNamespace> FindNamespace(const std::string &nspaceStr); 122 123 AutoPtr<ASTNamespace> GetNamespace(size_t index); 124 GetNamespace()125 inline std::vector<AutoPtr<ASTNamespace>> GetNamespace() 126 { 127 return namespaces_; 128 } 129 GetNamespaceNumber()130 inline size_t GetNamespaceNumber() 131 { 132 return namespaces_.size(); 133 } 134 135 void AddInterfaceDef(const AutoPtr<ASTInterfaceType> &interface); 136 137 AutoPtr<ASTInterfaceType> GetInterfaceDef(size_t index = 0); 138 GetInterfaceDefNumber()139 inline size_t GetInterfaceDefNumber() const 140 { 141 return interfaceDefs_.size(); 142 } 143 144 void AddSequenceableDef(const AutoPtr<ASTSequenceableType> &sequenceable); 145 146 AutoPtr<ASTSequenceableType> GetSequenceableDef(size_t index = 0); 147 GetSequenceableDefNumber()148 inline size_t GetSequenceableDefNumber() const 149 { 150 return sequenceableDefs_.size(); 151 } 152 153 void AddRawDataDef(const AutoPtr<ASTRawDataType> &rawdata); 154 155 AutoPtr<ASTRawDataType> GetRawDataDef(size_t index = 0); 156 GetRawDataDefNumber()157 inline size_t GetRawDataDefNumber() const 158 { 159 return rawdataDefs_.size(); 160 } 161 162 int IndexOf(ASTInterfaceType* interface); 163 164 int IndexOf(ASTSequenceableType* sequenceable); 165 166 int IndexOf(ASTRawDataType* rawdata); 167 168 int IndexOf(ASTType* type); 169 170 void AddType(const AutoPtr<ASTType> &type); 171 172 AutoPtr<ASTType> FindType(const std::string &typeName, bool lookImports = true); 173 GetTypes()174 inline const TypeStringMap &GetTypes() const 175 { 176 return types_; 177 } 178 GetTypeNumber()179 inline size_t GetTypeNumber() const 180 { 181 return types_.size(); 182 } 183 184 void AddTypeDefinition(const AutoPtr<ASTType> &type); 185 GetTypeDefinitionNumber()186 inline size_t GetTypeDefinitionNumber() const 187 { 188 return typeDefinitions_.size(); 189 } 190 191 AutoPtr<ASTType> GetTypeDefintion(size_t index); 192 193 std::string Dump(const std::string &prefix) override; 194 195 bool AddImport(const AutoPtr<AST> &importAst); 196 197 void AddImportName(const std::string &importName); 198 ClearImport()199 void ClearImport() 200 { 201 return imports_.clear(); 202 } 203 GetImports()204 inline const StrASTMap &GetImports() const 205 { 206 return imports_; 207 } 208 GetImportNames()209 inline const std::vector<std::string> &GetImportNames() const 210 { 211 return importNames_; 212 } 213 214 void SetVersion(size_t &majorVer, size_t &minorVer); 215 GetMajorVer()216 inline size_t GetMajorVer() const 217 { 218 return majorVersion_; 219 } 220 GetMinorVer()221 inline size_t GetMinorVer() const 222 { 223 return minorVersion_; 224 } 225 GetVersion()226 std::string GetVersion() const 227 { 228 return StringHelper::Format("%u.%u", majorVersion_, minorVersion_); 229 } 230 GetIdlFilePath()231 inline std::string GetIdlFilePath() 232 { 233 return idlFilePath_; 234 } 235 236 bool IsValid(); GetInterfaceDefs()237 std::vector<AutoPtr<ASTInterfaceType>> GetInterfaceDefs() const 238 { 239 return interfaceDefs_; 240 } 241 GetHasCacheableProxyMethods()242 bool GetHasCacheableProxyMethods() const 243 { 244 return hasCacheableProxyMethods_; 245 } 246 SetHasCacheableProxyMethods(bool cacheable)247 void SetHasCacheableProxyMethods(bool cacheable) 248 { 249 hasCacheableProxyMethods_ = cacheable; 250 } 251 SetInterfaceToken(std::string & interfaceToken)252 inline void SetInterfaceToken(std::string &interfaceToken) 253 { 254 interfaceToken_ = interfaceToken; 255 } 256 GetInterfaceToken()257 inline std::string GetInterfaceToken() const 258 { 259 return interfaceToken_; 260 } 261 SetSupportDelegator(std::string & supportDelegator)262 inline void SetSupportDelegator(std::string &supportDelegator) 263 { 264 if (supportDelegator == "on") { 265 supportDelegatorOn_ = true; 266 } else { 267 supportDelegatorOn_ = false; 268 } 269 } 270 GetSupportDelegatorOn()271 inline bool GetSupportDelegatorOn() const 272 { 273 return supportDelegatorOn_; 274 } 275 SetOptionStubHooks(std::string & optionStubHooks)276 inline void SetOptionStubHooks(std::string &optionStubHooks) 277 { 278 if (optionStubHooks == "on") { 279 optionStubHooksOn_ = true; 280 } else { 281 optionStubHooksOn_ = false; 282 } 283 } 284 GetOptionStubHooksOn()285 inline bool GetOptionStubHooksOn() const 286 { 287 return optionStubHooksOn_; 288 } 289 SetOptionParcelHooks(std::string & optionParcelHooks)290 inline void SetOptionParcelHooks(std::string &optionParcelHooks) 291 { 292 if (optionParcelHooks == "on") { 293 optionParcelHooksOn_ = true; 294 } else { 295 optionParcelHooksOn_ = false; 296 } 297 } 298 GetOptionParcelHooksOn()299 inline bool GetOptionParcelHooksOn() const 300 { 301 return optionParcelHooksOn_; 302 } 303 304 private: 305 AutoPtr<ASTNamespace> NewNameSpace(std::string nameSpace); 306 307 ASTFileType astFileType_ = ASTFileType::AST_IFACE; 308 std::string name_; 309 std::string license_; 310 std::string packageName_; 311 size_t majorVersion_; 312 size_t minorVersion_; 313 std::vector<AutoPtr<ASTNamespace>> namespaces_; 314 std::vector<AutoPtr<ASTType>> typeDefinitions_; // enum, struct, union 315 std::vector<AutoPtr<ASTSequenceableType>> sequenceableDefs_; 316 std::vector<AutoPtr<ASTRawDataType>> rawdataDefs_; 317 std::vector<AutoPtr<ASTInterfaceType>> interfaceDefs_; 318 std::vector<std::string> importNames_; 319 320 StrASTMap imports_; 321 TypeStringMap types_; 322 323 static TypeStringMap basicTypes_; 324 325 std::string idlFilePath_; 326 bool hasCacheableProxyMethods_ = false; 327 std::string interfaceToken_; 328 bool supportDelegatorOn_ = false; 329 bool optionStubHooksOn_ = false; 330 bool optionParcelHooksOn_ = false; 331 }; 332 } // namespace Idl 333 } // namespace OHOS 334 335 #endif // OHOS_IDL_AST_H