Method.h revision ff5e64ae0c65b80e2baa4a95b9ae9ee894b60080
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 "Location.h"
32#include "Reference.h"
33
34namespace android {
35
36struct Annotation;
37struct ConstantExpression;
38struct Formatter;
39struct ScalarType;
40struct Type;
41struct TypedVarVector;
42
43enum MethodImplType {
44    IMPL_INTERFACE,
45    IMPL_PROXY,
46    IMPL_STUB, // overrides the code in onTransact; IMPL_STUB_IMPL will be ignored
47    IMPL_STUB_IMPL, // use this->method() instead of mImpl->method()
48    IMPL_PASSTHROUGH,
49};
50
51using MethodImpl = std::map<MethodImplType, std::function<void(Formatter &)>>;
52
53struct Method {
54    Method(const char* name, std::vector<NamedReference<Type>*>* args,
55           std::vector<NamedReference<Type>*>* results, bool oneway,
56           std::vector<Annotation*>* annotations, const Location& location);
57
58    std::string name() const;
59    const std::vector<NamedReference<Type>*>& args() const;
60    const std::vector<NamedReference<Type>*>& results() const;
61    bool isOneway() const { return mOneway; }
62    bool overridesCppImpl(MethodImplType type) const;
63    bool overridesJavaImpl(MethodImplType type) const;
64    void cppImpl(MethodImplType type, Formatter &out) const;
65    void javaImpl(MethodImplType type, Formatter &out) const;
66    bool isHidlReserved() const { return mIsHidlReserved; }
67    bool isHiddenFromJava() const;
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
132struct 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