• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_ASTMETHOD_H
17 #define OHOS_IDL_ASTMETHOD_H
18 
19 #include <vector>
20 
21 #include "ast/ast_node.h"
22 #include "ast/ast_parameter.h"
23 #include "util/autoptr.h"
24 
25 namespace OHOS {
26 namespace Idl {
27 class ASTMethod : public ASTNode {
28 public:
SetName(const std::string & name)29     inline void SetName(const std::string &name)
30     {
31         name_ = name;
32     }
33 
GetName()34     inline std::string GetName()
35     {
36         return name_;
37     }
38 
39     std::string GetSignature();
40 
SetAttribute(const AutoPtr<ASTAttr> & attr)41     inline void SetAttribute(const AutoPtr<ASTAttr>& attr)
42     {
43         if (attr_ != nullptr && attr != nullptr) {
44             attr_->SetValue(attr->GetValue());
45         }
46     }
47 
GetAttribute()48     inline AutoPtr<ASTAttr> GetAttribute() const
49     {
50         return attr_;
51     }
52 
SetReturnType(AutoPtr<ASTType> type)53     inline void SetReturnType(AutoPtr<ASTType> type)
54     {
55         returnType_ = type;
56     }
57 
GetReturnType()58     inline AutoPtr<ASTType> GetReturnType()
59     {
60         return returnType_;
61     }
62 
IsOneWay()63     inline bool IsOneWay() const
64     {
65         return attr_->HasValue(ASTAttr::ONEWAY);
66     }
67 
IsFull()68     inline bool IsFull() const
69     {
70         return attr_->HasValue(ASTAttr::FULL);
71     }
72 
IsLite()73     inline bool IsLite() const
74     {
75         return attr_->HasValue(ASTAttr::LITE);
76     }
77 
IsMini()78     inline bool IsMini() const
79     {
80         return attr_->HasValue(ASTAttr::MINI);
81     }
82 
IsOverload()83     inline bool IsOverload() const
84     {
85         return isOverload_;
86     }
87 
88     void CheckOverload(AutoPtr<ASTInterfaceType> interface);
89 
90     void AddParameter(const AutoPtr<ASTParameter> &parameter);
91 
92     AutoPtr<ASTParameter> GetParameter(size_t index);
93 
GetParameterNumber()94     inline size_t GetParameterNumber()
95     {
96         return parameters_.size();
97     }
98 
SetCmdId(size_t cmdId)99     inline void SetCmdId(size_t cmdId)
100     {
101         cmdId_ = cmdId;
102     }
103 
GetCmdId()104     inline size_t GetCmdId() const
105     {
106         return cmdId_;
107     }
108 
GetMethodIdentifier()109     inline std::string GetMethodIdentifier() const
110     {
111         return isOverload_ ? "_" + std::to_string(cmdId_) : "";
112     }
113 
114     std::string Dump(const std::string &prefix) override;
115 
SetCacheable(const AutoPtr<ASTAttr> & attr)116     void SetCacheable(const AutoPtr<ASTAttr>& attr)
117     {
118         if (attr->HasValue(ASTAttr::CACHEABLE)) {
119             attr_->SetValue(ASTAttr::CACHEABLE);
120             attr_->SetCacheableTimeString(attr->GetCacheableTimeString());
121         }
122     }
123 
SetCacheableTime()124     bool SetCacheableTime()
125     {
126         return attr_->CacheableStrToInt();
127     }
128 
GetCacheableTime()129     int32_t GetCacheableTime()
130     {
131         return attr_->GetCacheableTime();
132     }
133 
GetCacheable()134     bool GetCacheable()
135     {
136         return attr_->HasValue(ASTAttr::CACHEABLE);
137     }
138 
SetFreezeControlReason(const std::string & reason)139     inline void SetFreezeControlReason(const std::string &reason)
140     {
141         freezeControlReason = reason;
142     }
143 
GetFreezeControlReason()144     inline std::string GetFreezeControlReason()
145     {
146         return freezeControlReason;
147     }
148 
IsFreezeControl()149     inline bool IsFreezeControl() const
150     {
151         return attr_->HasValue(ASTAttr::FREEZECONTROL);
152     }
153 
SetMessageOption(const std::string & messageOption)154     inline void SetMessageOption(const std::string &messageOption)
155     {
156         messageOption_ = messageOption;
157         if (StringHelper::Split(messageOption, "TF_ASYNC").size() > 1) {
158             attr_->SetValue(ASTAttr::ONEWAY);
159         }
160     }
161 
GetMessageOption()162     inline std::string GetMessageOption()
163     {
164         return messageOption_;
165     }
166 
IsMessageOption()167     inline bool IsMessageOption() const
168     {
169         return attr_->HasValue(ASTAttr::CUSTOM_MSG_OPTION);
170     }
171 
HasIpcCode()172     bool HasIpcCode()
173     {
174         return attr_->HasValue(ASTAttr::IPCCODE);
175     }
176 
GetIpcCodeStr()177     std::string& GetIpcCodeStr()
178     {
179         return attr_->GetIpcCodeStr();
180     }
181 
SetIpcCode(const AutoPtr<ASTAttr> & attr)182     void SetIpcCode(const AutoPtr<ASTAttr> &attr)
183     {
184         if (attr->HasValue(ASTAttr::IPCCODE)) {
185             attr_->SetIpcCodeStr(attr->GetIpcCodeStr());
186         }
187     }
188 
HasIpcInCapacity()189     bool HasIpcInCapacity()
190     {
191         return attr_->HasValue(ASTAttr::IPC_IN_CAPACITY);
192     }
193 
GetIpcInCapacity()194     std::string &GetIpcInCapacity()
195     {
196         return attr_->GetIpcInCapacity();
197     }
198 
SetIpcInCapacity(const AutoPtr<ASTAttr> & attr)199     void SetIpcInCapacity(const AutoPtr<ASTAttr> &attr)
200     {
201         if (attr->HasValue(ASTAttr::IPC_IN_CAPACITY)) {
202             attr_->SetIpcInCapacity(attr->GetIpcInCapacity());
203         }
204     }
205 
HasIpcOutCapacity()206     bool HasIpcOutCapacity()
207     {
208         return attr_->HasValue(ASTAttr::IPC_OUT_CAPACITY);
209     }
210 
GetIpcOutCapacity()211     std::string &GetIpcOutCapacity()
212     {
213         return attr_->GetIpcOutCapacity();
214     }
215 
SetIpcOutCapacity(const AutoPtr<ASTAttr> & attr)216     void SetIpcOutCapacity(const AutoPtr<ASTAttr> &attr)
217     {
218         if (attr->HasValue(ASTAttr::IPC_OUT_CAPACITY)) {
219             attr_->SetIpcOutCapacity(attr->GetIpcOutCapacity());
220         }
221     }
222 
SetIpcCode(int32_t ipc_code)223     void SetIpcCode(int32_t ipc_code)
224     {
225         this->ipcCode_ = ipc_code;
226     }
227 
GetIpcCode()228     int32_t GetIpcCode() const
229     {
230         return this->ipcCode_;
231     }
232 
IsMacro()233     inline bool IsMacro()
234     {
235         return attr_->HasValue(ASTAttr::MACRO);
236     }
237 
SetMacroVal(const std::string & macroVal)238     inline void SetMacroVal(const std::string& macroVal)
239     {
240         macroVal_ = macroVal;
241     }
242 
GetMacroVal()243     inline std::string GetMacroVal()
244     {
245         return macroVal_;
246     }
247 
SetMacroType(const std::string & macroType)248     inline void SetMacroType(const std::string& macroType)
249     {
250         macroType_ = macroType;
251     }
252 
GetMacroType()253     inline std::string GetMacroType()
254     {
255         return macroType_;
256     }
257 
258 private:
259     void BuildSignature();
260 
261     std::string name_;
262     std::string signature_;
263     AutoPtr<ASTAttr> attr_ = new ASTAttr();
264     AutoPtr<ASTType> returnType_;
265     std::vector<AutoPtr<ASTParameter>> parameters_;
266     bool isOverload_ = false; // used to identify if method is overload
267     size_t cmdId_ = 0;        // used to identify same name method
268     int32_t ipcCode_ = 0;
269     std::string freezeControlReason;
270     std::string messageOption_;
271     std::string macroVal_;
272     std::string macroType_;
273 };
274 } // namespace Idl
275 } // namespace OHOS
276 
277 #endif // OHOS_IDL_ASTMETHOD_H
278