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