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