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