• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 char* 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     std::vector<ConstantExpression*> getConstantExpressions();
77     std::vector<const ConstantExpression*> getConstantExpressions() const;
78 
79     // Make a copy with the same name, args, results, oneway, annotations.
80     // Implementations, serial are not copied.
81     Method *copySignature() const;
82 
83     void setSerialId(size_t serial);
84     size_t getSerialId() const;
85 
86     // Fill implementation for HIDL reserved methods. mIsHidlReserved will be
87     // set to true.
88     void fillImplementation(
89             size_t serial,
90             MethodImpl cppImpl,
91             MethodImpl javaImpl);
92 
93     void generateCppReturnType(Formatter &out, bool specifyNamespaces = true) const;
94     void generateCppSignature(Formatter &out,
95                               const std::string &className = "",
96                               bool specifyNamespaces = true) const;
97 
98     bool hasEmptyCppArgSignature() const;
99     void emitCppArgSignature(Formatter &out, bool specifyNamespaces = true) const;
100     void emitCppResultSignature(Formatter &out, bool specifyNamespaces = true) const;
101 
102     void emitJavaArgSignature(Formatter &out) const;
103     void emitJavaResultSignature(Formatter &out) const;
104 
105     const NamedReference<Type>* canElideCallback() const;
106 
107     void dumpAnnotations(Formatter &out) const;
108 
109     bool deepIsJavaCompatible(std::unordered_set<const Type*>* visited) const;
110 
111     const Location& location() const;
112 
113    private:
114     std::string mName;
115     size_t mSerial = 0;
116     std::vector<NamedReference<Type>*>* mArgs;
117     std::vector<NamedReference<Type>*>* mResults;
118     bool mOneway;
119     std::vector<Annotation *> *mAnnotations;
120 
121     bool mIsHidlReserved = false;
122     // The following fields have no meaning if mIsHidlReserved is false.
123     // hard-coded implementation for HIDL reserved methods.
124     MethodImpl mCppImpl;
125     MethodImpl mJavaImpl;
126 
127     const Location mLocation;
128 
129     DISALLOW_COPY_AND_ASSIGN(Method);
130 };
131 
132 struct TypedVarVector : public std::vector<NamedReference<Type>*> {
133     TypedVarVector() = default;
134 
135     bool add(NamedReference<Type>* v);
136 
137    private:
138     std::set<std::string> mNames;
139 };
140 
141 }  // namespace android
142 
143 #endif  // METHOD_H_
144 
145