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