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