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(AutoPtr<ASTAttr> attr)41     inline void SetAttribute(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()
105     {
106         return cmdId_;
107     }
108 
GetMethodIdentifier()109     inline std::string GetMethodIdentifier()
110     {
111         return isOverload_ ? "_" + std::to_string(cmdId_) : "";
112     }
113 
114     std::string Dump(const std::string &prefix) override;
115 
SetCacheable(AutoPtr<ASTAttr> attr)116     void SetCacheable(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 
154 private:
155     void BuildSignature();
156 
157     std::string name_;
158     std::string signature_;
159     AutoPtr<ASTAttr> attr_ = new ASTAttr();
160     AutoPtr<ASTType> returnType_;
161     std::vector<AutoPtr<ASTParameter>> parameters_;
162     bool isOverload_ = false;  // used to identify if method is overload
163     size_t cmdId_;  // used to identify same name method
164     std::string freezeControlReason;
165 };
166 } // namespace Idl
167 } // namespace OHOS
168 
169 #endif // OHOS_IDL_ASTMETHOD_H
170