1 /*
2  * Copyright (c) Facebook, Inc. and its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <memory>
20 #include <string>
21 
22 #include <thrift/compiler/ast/node_list.h>
23 #include <thrift/compiler/ast/t_named.h>
24 #include <thrift/compiler/ast/t_node.h>
25 #include <thrift/compiler/ast/t_paramlist.h>
26 #include <thrift/compiler/ast/t_throws.h>
27 #include <thrift/compiler/ast/t_type.h>
28 
29 namespace apache {
30 namespace thrift {
31 namespace compiler {
32 
33 enum class t_function_qualifier {
34   unspecified = 0,
35   one_way,
36   idempotent,
37   read_only,
38 };
39 
40 /**
41  * A thrift function declaration.
42  */
43 class t_function final : public t_named {
44  public:
45   /**
46    * Constructor for t_function
47    *
48    * @param return_type      - The type of the value that will be returned
49    * @param name             - The symbolic name of the function
50    * @param paramlist        - The parameters that are passed to the functions
51    */
52   t_function(
53       t_type_ref return_type,
54       std::string name,
55       std::unique_ptr<t_paramlist> paramlist,
56       t_function_qualifier qualifier = {});
57 
return_type()58   const t_type_ref& return_type() const { return return_type_; }
params()59   const t_paramlist* params() const { return paramlist_.get(); }
60 
61   // The qualifier of the function, if any.
qualifier()62   t_function_qualifier qualifier() const { return qualifier_; }
63 
64   // The declared exceptions that function might throw.
65   //
66   // Returns nullptr when the throws clause is absent.
exceptions()67   t_throws* exceptions() { return exceptions_.get(); }
exceptions()68   const t_throws* exceptions() const { return exceptions_.get(); }
69   // Use nullptr to indicate an absent throws clause.
70   void set_exceptions(std::unique_ptr<t_throws> exceptions);
71 
is_interaction_constructor()72   bool is_interaction_constructor() const { return isInteractionConstructor_; }
set_is_interaction_constructor()73   void set_is_interaction_constructor() { isInteractionConstructor_ = true; }
is_interaction_member()74   bool is_interaction_member() const { return isInteractionMember_; }
set_is_interaction_member()75   void set_is_interaction_member() { isInteractionMember_ = true; }
76 
77  private:
78   t_type_ref return_type_;
79   std::unique_ptr<t_paramlist> paramlist_;
80   std::unique_ptr<t_throws> exceptions_;
81   const t_function_qualifier qualifier_;
82   bool isInteractionConstructor_{false};
83   bool isInteractionMember_{false};
84 
85   // TODO(afuller): Delete everything below here. It is only provided for
86   // backwards compatibility.
87  public:
88   t_function(
89       const t_type* return_type,
90       std::string name,
91       std::unique_ptr<t_paramlist> paramlist,
92       std::unique_ptr<t_throws> exceptions = nullptr,
93       t_function_qualifier qualifier = {})
t_function(t_type_ref::from_req_ptr (return_type),std::move (name),std::move (paramlist),qualifier)94       : t_function(
95             t_type_ref::from_req_ptr(return_type),
96             std::move(name),
97             std::move(paramlist),
98             qualifier) {
99     set_exceptions(std::move(exceptions));
100   }
101 
get_paramlist()102   t_paramlist* get_paramlist() const { return paramlist_.get(); }
get_return_type()103   const t_type* get_return_type() const { return return_type().get_type(); }
get_returntype()104   const t_type* get_returntype() const { return return_type().get_type(); }
get_xceptions()105   const t_throws* get_xceptions() const {
106     return t_throws::or_empty(exceptions());
107   }
get_stream_xceptions()108   const t_throws* get_stream_xceptions() const {
109     return t_throws::or_empty(stream_exceptions_);
110   }
get_sink_xceptions()111   const t_throws* get_sink_xceptions() const {
112     return t_throws::or_empty(sink_exceptions_);
113   }
get_sink_final_response_xceptions()114   const t_throws* get_sink_final_response_xceptions() const {
115     return t_throws::or_empty(sink_final_response_exceptions_);
116   }
is_oneway()117   bool is_oneway() const { return qualifier_ == t_function_qualifier::one_way; }
returns_stream()118   bool returns_stream() const { return return_type_->is_streamresponse(); }
returns_sink()119   bool returns_sink() const { return return_type_->is_sink(); }
120 
121  private:
122   // Extracted from return type for easy access.
123   const t_throws* stream_exceptions_ = nullptr;
124   const t_throws* sink_exceptions_ = nullptr;
125   const t_throws* sink_final_response_exceptions_ = nullptr;
126 };
127 
128 using t_function_list = node_list<t_function>;
129 
130 } // namespace compiler
131 } // namespace thrift
132 } // namespace apache
133