1 /*
2  * Copyright (C) 2016 The Android Open Source Project
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 #ifndef METHOD_H_
18 
19 #define METHOD_H_
20 
21 #include <android-base/macros.h>
22 #include <hidl-util/Formatter.h>
23 #include <utils/Errors.h>
24 #include <functional>
25 #include <map>
26 #include <set>
27 #include <string>
28 #include <unordered_set>
29 #include <vector>
30 
31 #include "DocComment.h"
32 #include "Location.h"
33 #include "Reference.h"
34 
35 namespace android {
36 
37 struct Annotation;
38 struct ConstantExpression;
39 struct Formatter;
40 struct ScalarType;
41 struct Type;
42 struct TypedVarVector;
43 
44 enum MethodImplType {
45     IMPL_INTERFACE,
46     IMPL_PROXY,
47     IMPL_STUB, // overrides the code in onTransact; IMPL_STUB_IMPL will be ignored
48     IMPL_STUB_IMPL, // use this->method() instead of mImpl->method()
49     IMPL_PASSTHROUGH,
50 };
51 
52 using MethodImpl = std::map<MethodImplType, std::function<void(Formatter &)>>;
53 
54 struct Method : DocCommentable {
55     Method(const std::string& name, std::vector<NamedReference<Type>*>* args,
56            std::vector<NamedReference<Type>*>* results, bool oneway,
57            std::vector<Annotation*>* annotations, const Location& location);
58 
59     std::string name() const;
60     const std::vector<NamedReference<Type>*>& args() const;
61     const std::vector<NamedReference<Type>*>& results() const;
isOnewayMethod62     bool isOneway() const { return mOneway; }
63     bool overridesCppImpl(MethodImplType type) const;
64     bool overridesJavaImpl(MethodImplType type) const;
65     void cppImpl(MethodImplType type, Formatter &out) const;
66     void javaImpl(MethodImplType type, Formatter &out) const;
isHidlReservedMethod67     bool isHidlReserved() const { return mIsHidlReserved; }
68     const std::vector<Annotation *> &annotations() const;
69 
70     std::vector<Reference<Type>*> getReferences();
71     std::vector<const Reference<Type>*> getReferences() const;
72 
73     std::vector<Reference<Type>*> getStrongReferences();
74     std::vector<const Reference<Type>*> getStrongReferences() const;
75 
76     // Make a copy with the same name, args, results, oneway, annotations.
77     // Implementations, serial are not copied.
78     Method *copySignature() const;
79 
80     void setSerialId(size_t serial);
81     size_t getSerialId() const;
82 
83     // Fill implementation for HIDL reserved methods. mIsHidlReserved will be
84     // set to true.
85     void fillImplementation(
86             size_t serial,
87             MethodImpl cppImpl,
88             MethodImpl javaImpl);
89 
90     void generateCppReturnType(Formatter &out, bool specifyNamespaces = true) const;
91     void generateCppSignature(Formatter &out,
92                               const std::string &className = "",
93                               bool specifyNamespaces = true) const;
94 
95     bool hasEmptyCppArgSignature() const;
96     void emitCppArgSignature(Formatter &out, bool specifyNamespaces = true) const;
97     void emitCppResultSignature(Formatter &out, bool specifyNamespaces = true) const;
98 
99     void emitJavaArgSignature(Formatter &out) const;
100     void emitJavaResultSignature(Formatter &out) const;
101     void emitJavaSignature(Formatter& out) const;
102 
103     void emitHidlDefinition(Formatter& out) const;
104 
105     const NamedReference<Type>* canElideCallback() const;
106 
107     bool deepIsJavaCompatible(std::unordered_set<const Type*>* visited) const;
108 
109     const Location& location() const;
110 
111    private:
112     std::string mName;
113     size_t mSerial = 0;
114     std::vector<NamedReference<Type>*>* mArgs;
115     std::vector<NamedReference<Type>*>* mResults;
116     bool mOneway;
117     std::vector<Annotation *> *mAnnotations;
118 
119     bool mIsHidlReserved = false;
120     // The following fields have no meaning if mIsHidlReserved is false.
121     // hard-coded implementation for HIDL reserved methods.
122     MethodImpl mCppImpl;
123     MethodImpl mJavaImpl;
124 
125     const Location mLocation;
126 
127     DISALLOW_COPY_AND_ASSIGN(Method);
128 };
129 
130 struct TypedVarVector : public std::vector<NamedReference<Type>*> {
131     TypedVarVector() = default;
132 
133     bool add(NamedReference<Type>* v);
134 
135    private:
136     std::set<std::string> mNames;
137 };
138 
139 }  // namespace android
140 
141 #endif  // METHOD_H_
142 
143