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_ASTINTERFACETYPE_H
17 #define OHOS_IDL_ASTINTERFACETYPE_H
18 
19 #include "ast/ast_attribute.h"
20 #include "ast/ast_method.h"
21 
22 #include <vector>
23 
24 #include "ast/ast_type.h"
25 #include "util/autoptr.h"
26 
27 namespace OHOS {
28 namespace Idl {
29 class ASTInterfaceType : public ASTType {
30 public:
ASTInterfaceType()31     ASTInterfaceType()
32         : ASTType(false),
33         license_(),
34         attr_(new ASTAttr()),
35         isSerializable_(false),
36         methods_(),
37         getVerMethod_(),
38         extendsInterface_(nullptr),
39         majorVersion_(1),
40         minorVersion_(0),
41         isExternal_(false)
42     {
43     }
44 
45     void SetNamespace(const AutoPtr<ASTNamespace> &nspace) override;
46 
SetLicense(const std::string & license)47     inline void SetLicense(const std::string &license)
48     {
49         license_ = license;
50     }
51 
GetLicense()52     inline std::string GetLicense() const
53     {
54         return license_;
55     }
56 
SetAttribute(const AutoPtr<ASTAttr> & attr)57     void SetAttribute(const AutoPtr<ASTAttr> &attr)
58     {
59         if (attr != nullptr) {
60             attr_ = attr;
61             if (attr_->HasValue(ASTAttr::CALLBACK)) {
62                 isSerializable_ = true;
63             }
64         }
65     }
66 
GetAttribute()67     inline AutoPtr<ASTAttr> GetAttribute() const
68     {
69         return attr_;
70     }
71 
IsAttributeNone()72     inline bool IsAttributeNone() const
73     {
74         return attr_->IsNone();
75     }
76 
IsOneWay()77     inline bool IsOneWay() const
78     {
79         return attr_->HasValue(ASTAttr::ONEWAY);
80     }
81 
IsCallback()82     inline bool IsCallback() const
83     {
84         return attr_->HasValue(ASTAttr::CALLBACK);
85     }
86 
SetSerializable(bool isSerializable)87     inline void SetSerializable(bool isSerializable)
88     {
89         isSerializable_ = isSerializable;
90     }
91 
IsSerializable()92     inline bool IsSerializable() const
93     {
94         return isSerializable_;
95     }
96 
IsFull()97     inline bool IsFull() const
98     {
99         return attr_->HasValue(ASTAttr::FULL);
100     }
101 
IsLite()102     inline bool IsLite() const
103     {
104         return attr_->HasValue(ASTAttr::LITE);
105     }
106 
IsMini()107     inline bool IsMini() const
108     {
109         return attr_->HasValue(ASTAttr::MINI);
110     }
111 
112     void AddMethod(const AutoPtr<ASTMethod> &method);
113 
114     AutoPtr<ASTMethod> GetMethod(size_t index);
115 
116     std::vector<AutoPtr<ASTMethod>> GetMethods() const;
117 
118     std::vector<AutoPtr<ASTMethod>> GetMethodsBySystem(SystemLevel system) const;
119 
GetMethodNumber()120     inline size_t GetMethodNumber() const
121     {
122         return methods_.size();
123     }
124 
AddVersionMethod(const AutoPtr<ASTMethod> & method)125     void AddVersionMethod(const AutoPtr<ASTMethod> &method)
126     {
127         getVerMethod_ = method;
128     }
129 
GetVersionMethod()130     AutoPtr<ASTMethod> GetVersionMethod()
131     {
132         return getVerMethod_;
133     }
134 
135     bool AddExtendsInterface(AutoPtr<ASTInterfaceType> interface);
136 
GetExtendsInterface()137     AutoPtr<ASTInterfaceType> GetExtendsInterface()
138     {
139         return extendsInterface_;
140     }
141 
GetMajorVersion()142     inline size_t GetMajorVersion()
143     {
144         return majorVersion_;
145     }
146 
GetMinorVersion()147     inline size_t GetMinorVersion()
148     {
149         return minorVersion_;
150     }
151 
SetExternal(bool external)152     inline void SetExternal(bool external)
153     {
154         isExternal_ = external;
155     }
156 
IsExternal()157     inline bool IsExternal() const
158     {
159         return isExternal_;
160     }
161 
162     void SetVersion(size_t majorVer, size_t minorVer);
163 
164     std::string GetSignature() override;
165 
166     bool IsInterfaceType() override;
167 
168     std::string Dump(const std::string &prefix) override;
169 
170     TypeKind GetTypeKind() override;
171 
172     std::string GetFullName() const;
173 
174 private:
175     std::string license_;
176 
177     AutoPtr<ASTAttr> attr_;
178     bool isSerializable_;
179     std::vector<AutoPtr<ASTMethod>> methods_;
180     AutoPtr<ASTMethod> getVerMethod_;
181     AutoPtr<ASTInterfaceType> extendsInterface_;
182     size_t majorVersion_;
183     size_t minorVersion_;
184     bool isExternal_;
185 };
186 } // namespace Idl
187 } // namespace OHOS
188 
189 #endif // OHOS_IDL_ASTINTERFACETYPE_H