• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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_AST_H
10 #define OHOS_HDI_AST_H
11 
12 #include <unordered_map>
13 #include <vector>
14 
15 #include "ast/ast_array_type.h"
16 #include "ast/ast_boolean_type.h"
17 #include "ast/ast_buffer_handle_type.h"
18 #include "ast/ast_byte_type.h"
19 #include "ast/ast_double_type.h"
20 #include "ast/ast_enum_type.h"
21 #include "ast/ast_fd_type.h"
22 #include "ast/ast_float_type.h"
23 #include "ast/ast_integer_type.h"
24 #include "ast/ast_interface_type.h"
25 #include "ast/ast_long_type.h"
26 #include "ast/ast_map_type.h"
27 #include "ast/ast_namespace.h"
28 #include "ast/ast_node.h"
29 #include "ast/ast_sequenceable_type.h"
30 #include "ast/ast_short_type.h"
31 #include "ast/ast_smq_type.h"
32 #include "ast/ast_string_type.h"
33 #include "ast/ast_struct_type.h"
34 #include "ast/ast_uchar_type.h"
35 #include "ast/ast_uint_type.h"
36 #include "ast/ast_ulong_type.h"
37 #include "ast/ast_union_type.h"
38 #include "ast/ast_ushort_type.h"
39 #include "ast/ast_void_type.h"
40 #include "util/autoptr.h"
41 
42 namespace OHOS {
43 namespace HDI {
44 enum class ASTFileType {
45     AST_IFACE,        // this idl file contains class of normal interface
46     AST_ICALLBACK,    // this idl file contains class of callback interface
47     AST_TYPES,        // this idl file contains custom types
48     AST_SEQUENCEABLE, // this is not an idl file, but a c++/java file
49 };
50 
51 class AST : public ASTNode {
52 public:
53     using StrASTMap = std::unordered_map<std::string, AutoPtr<AST>>;
54     using TypeStringMap = std::unordered_map<std::string, AutoPtr<ASTType>>;
55 
56     virtual ~AST() = default;
57 
SetAStFileType(ASTFileType fileType)58     void SetAStFileType(ASTFileType fileType)
59     {
60         astFileType_ = fileType;
61     }
62 
GetASTFileType()63     ASTFileType GetASTFileType()
64     {
65         return astFileType_;
66     }
67 
68     void SetIdlFile(const std::string &idlFile);
69 
GetName()70     inline std::string GetName()
71     {
72         return name_;
73     }
74 
75     void SetFullName(const std::string &fullName);
76 
GetFullName()77     inline std::string GetFullName()
78     {
79         return packageName_ + "." + name_;
80     }
81 
SetLicense(const std::string & license)82     inline void SetLicense(const std::string &license)
83     {
84         license_ = license;
85     }
86 
GetLicense()87     inline std::string GetLicense()
88     {
89         return license_;
90     }
91 
92     void SetPackageName(const std::string &packageName);
93 
94     std::string GetPackageName();
95 
96     AutoPtr<ASTNamespace> ParseNamespace(const std::string &nspaceStr);
97 
98     void AddNamespace(const AutoPtr<ASTNamespace> &nspace);
99 
100     AutoPtr<ASTNamespace> FindNamespace(const std::string &nspaceStr);
101 
102     AutoPtr<ASTNamespace> GetNamespace(size_t index);
103 
GetNamespaceNumber()104     inline size_t GetNamespaceNumber()
105     {
106         return namespaces_.size();
107     }
108 
109     void AddInterfaceDef(const AutoPtr<ASTInterfaceType> &interface);
110 
GetInterfaceDef()111     inline AutoPtr<ASTInterfaceType> GetInterfaceDef()
112     {
113         return interfaceDef_;
114     }
115 
116     void AddSequenceableDef(const AutoPtr<ASTSequenceableType> &sequenceable);
117 
GetSequenceableDef()118     inline AutoPtr<ASTSequenceableType> GetSequenceableDef()
119     {
120         return sequenceableDef_;
121     }
122 
123     void AddType(const AutoPtr<ASTType> &type);
124 
125     AutoPtr<ASTType> FindType(const std::string &typeName);
126 
GetTypes()127     inline const TypeStringMap &GetTypes() const
128     {
129         return types_;
130     }
131 
GetTypeNumber()132     inline size_t GetTypeNumber() const
133     {
134         return types_.size();
135     }
136 
137     void AddTypeDefinition(const AutoPtr<ASTType> &type);
138 
GetTypeDefinitionNumber()139     inline size_t GetTypeDefinitionNumber() const
140     {
141         return typeDefinitions_.size();
142     }
143 
144     AutoPtr<ASTType> GetTypeDefintion(size_t index);
145 
146     std::string Dump(const std::string &prefix) override;
147 
148     bool AddImport(const AutoPtr<AST> &importAst);
149 
GetImports()150     inline const StrASTMap &GetImports() const
151     {
152         return imports_;
153     }
154 
155     void SetVersion(size_t &majorVer, size_t &minorVer);
156 
GetMajorVer()157     inline size_t GetMajorVer()
158     {
159         return majorVersion_;
160     }
161 
GetMinorVer()162     inline size_t GetMinorVer()
163     {
164         return minorVersion_;
165     }
166 
GetVersion()167     std::string GetVersion()
168     {
169         return StringHelper::Format("%u.%u", majorVersion_, minorVersion_);
170     }
171 
GetIdlFilePath()172     inline std::string GetIdlFilePath()
173     {
174         return idlFilePath_;
175     }
176 
177 private:
178     ASTFileType astFileType_ = ASTFileType::AST_IFACE;
179     std::string name_;
180     std::string license_;
181     std::string packageName_;
182     size_t majorVersion_;
183     size_t minorVersion_;
184     std::vector<AutoPtr<ASTNamespace>> namespaces_;
185     std::vector<AutoPtr<ASTType>> typeDefinitions_;
186     AutoPtr<ASTSequenceableType> sequenceableDef_ = nullptr;
187     AutoPtr<ASTInterfaceType> interfaceDef_ = nullptr;
188 
189     StrASTMap imports_;
190     TypeStringMap types_;
191 
192     static TypeStringMap basicTypes_;
193 
194     std::string idlFilePath_;
195 };
196 } // namespace HDI
197 } // namespace OHOS
198 
199 #endif // OHOS_HDI_AST_H