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