blob: e7ec4879cd0ff87e0227c82ec6d4ed5a7e51a023 [file] [log] [blame]
Andreas Huber1aec3972016-08-26 09:26:32 -07001/*
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
Andreas Huberc9410c72016-07-28 12:18:40 -070017#ifndef METHOD_H_
18
19#define METHOD_H_
20
21#include <android-base/macros.h>
Yifan Hong10fe0b52016-10-19 14:20:17 -070022#include <hidl-util/Formatter.h>
Timur Iskhakovcec46c42017-08-09 00:22:02 -070023#include <utils/Errors.h>
24#include <functional>
Martijn Coenen115d4282016-12-19 05:14:04 +010025#include <map>
Yifan Hong7763ab32016-12-13 17:42:11 -080026#include <set>
Andreas Huberc9410c72016-07-28 12:18:40 -070027#include <string>
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -070028#include <unordered_set>
Andreas Huber881227d2016-08-02 14:20:21 -070029#include <vector>
Andreas Huberc9410c72016-07-28 12:18:40 -070030
Steven Moreland073269e2018-05-17 15:45:26 -070031#include "DocComment.h"
Timur Iskhakovcec46c42017-08-09 00:22:02 -070032#include "Location.h"
Timur Iskhakov505316c2017-08-05 03:38:59 +000033#include "Reference.h"
34
Andreas Huberc9410c72016-07-28 12:18:40 -070035namespace android {
36
Andreas Huber3599d922016-08-09 10:42:57 -070037struct Annotation;
Timur Iskhakov891a8662017-08-25 21:53:48 -070038struct ConstantExpression;
Andreas Huberc9410c72016-07-28 12:18:40 -070039struct Formatter;
Iliyan Malchev40d474a2016-08-16 06:20:17 -070040struct ScalarType;
Andreas Huberc9410c72016-07-28 12:18:40 -070041struct Type;
Yifan Hong7763ab32016-12-13 17:42:11 -080042struct TypedVarVector;
Andreas Huberc9410c72016-07-28 12:18:40 -070043
Martijn Coenen115d4282016-12-19 05:14:04 +010044enum MethodImplType {
Steven Moreland937408a2017-03-20 09:54:18 -070045 IMPL_INTERFACE,
Martijn Coenen115d4282016-12-19 05:14:04 +010046 IMPL_PROXY,
Yifan Hongcd2ae452017-01-31 14:33:40 -080047 IMPL_STUB, // overrides the code in onTransact; IMPL_STUB_IMPL will be ignored
48 IMPL_STUB_IMPL, // use this->method() instead of mImpl->method()
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -080049 IMPL_PASSTHROUGH,
Martijn Coenen115d4282016-12-19 05:14:04 +010050};
51
52using MethodImpl = std::map<MethodImplType, std::function<void(Formatter &)>>;
Yifan Hong10fe0b52016-10-19 14:20:17 -070053
Steven Moreland073269e2018-05-17 15:45:26 -070054struct Method : DocCommentable {
Neel Mehta69920a62019-07-22 16:22:13 -070055 Method(const std::string& name, std::vector<NamedReference<Type>*>* args,
Timur Iskhakov7fa79f62017-08-09 11:04:54 -070056 std::vector<NamedReference<Type>*>* results, bool oneway,
Timur Iskhakovcec46c42017-08-09 00:22:02 -070057 std::vector<Annotation*>* annotations, const Location& location);
Andreas Huberc9410c72016-07-28 12:18:40 -070058
Andreas Huber881227d2016-08-02 14:20:21 -070059 std::string name() const;
Timur Iskhakov7fa79f62017-08-09 11:04:54 -070060 const std::vector<NamedReference<Type>*>& args() const;
61 const std::vector<NamedReference<Type>*>& results() const;
Iliyan Malchev639bff82016-08-13 14:24:11 -070062 bool isOneway() const { return mOneway; }
Martijn Coenen115d4282016-12-19 05:14:04 +010063 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;
Yifan Hong10fe0b52016-10-19 14:20:17 -070067 bool isHidlReserved() const { return mIsHidlReserved; }
Steven Morelandd537ab02016-09-12 10:32:01 -070068 const std::vector<Annotation *> &annotations() const;
Andreas Huber881227d2016-08-02 14:20:21 -070069
Timur Iskhakovb58f4182017-08-29 15:19:24 -070070 std::vector<Reference<Type>*> getReferences();
71 std::vector<const Reference<Type>*> getReferences() const;
Timur Iskhakov33431e62017-08-21 17:31:23 -070072
Timur Iskhakovff5e64a2017-09-11 14:56:18 -070073 std::vector<Reference<Type>*> getStrongReferences();
74 std::vector<const Reference<Type>*> getStrongReferences() const;
75
Yifan Hongffa91392017-01-31 13:41:23 -080076 // Make a copy with the same name, args, results, oneway, annotations.
77 // Implementations, serial are not copied.
78 Method *copySignature() const;
79
Steven Morelandef1a9fe2016-10-06 17:19:09 -070080 void setSerialId(size_t serial);
81 size_t getSerialId() const;
82
Yifan Hongffa91392017-01-31 13:41:23 -080083 // Fill implementation for HIDL reserved methods. mIsHidlReserved will be
84 // set to true.
85 void fillImplementation(
86 size_t serial,
87 MethodImpl cppImpl,
88 MethodImpl javaImpl);
89
Steven Morelandd8b10ee2017-07-31 15:06:20 -070090 void generateCppReturnType(Formatter &out, bool specifyNamespaces = true) const;
Steven Morelanda7a421a2016-09-07 08:35:18 -070091 void generateCppSignature(Formatter &out,
Yifan Hong068c5522016-10-31 14:07:25 -070092 const std::string &className = "",
93 bool specifyNamespaces = true) const;
Steven Morelanda7a421a2016-09-07 08:35:18 -070094
Steven Morelandd8b10ee2017-07-31 15:06:20 -070095 bool hasEmptyCppArgSignature() const;
96 void emitCppArgSignature(Formatter &out, bool specifyNamespaces = true) const;
97 void emitCppResultSignature(Formatter &out, bool specifyNamespaces = true) const;
98
Yifan Hong932464e2017-03-30 15:40:22 -070099 void emitJavaArgSignature(Formatter &out) const;
100 void emitJavaResultSignature(Formatter &out) const;
Neel Mehta5b447c02019-05-23 16:12:24 -0700101 void emitJavaSignature(Formatter& out) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700102
Neel Mehta3b414a82019-07-02 15:47:48 -0700103 void emitHidlDefinition(Formatter& out) const;
104
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700105 const NamedReference<Type>* canElideCallback() const;
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700106
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -0700107 bool deepIsJavaCompatible(std::unordered_set<const Type*>* visited) const;
Andreas Huber70a59e12016-08-16 12:57:01 -0700108
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700109 const Location& location() const;
110
111 private:
Andreas Huberc9410c72016-07-28 12:18:40 -0700112 std::string mName;
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700113 size_t mSerial = 0;
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700114 std::vector<NamedReference<Type>*>* mArgs;
115 std::vector<NamedReference<Type>*>* mResults;
Iliyan Malchev639bff82016-08-13 14:24:11 -0700116 bool mOneway;
Steven Morelandd537ab02016-09-12 10:32:01 -0700117 std::vector<Annotation *> *mAnnotations;
Andreas Huberc9410c72016-07-28 12:18:40 -0700118
Yifan Hong10fe0b52016-10-19 14:20:17 -0700119 bool mIsHidlReserved = false;
Martijn Coenen115d4282016-12-19 05:14:04 +0100120 // The following fields have no meaning if mIsHidlReserved is false.
Yifan Hong10fe0b52016-10-19 14:20:17 -0700121 // hard-coded implementation for HIDL reserved methods.
Martijn Coenen115d4282016-12-19 05:14:04 +0100122 MethodImpl mCppImpl;
123 MethodImpl mJavaImpl;
Yifan Hong10fe0b52016-10-19 14:20:17 -0700124
Timur Iskhakov532654e2017-09-07 14:03:18 -0700125 const Location mLocation;
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700126
Andreas Huberc9410c72016-07-28 12:18:40 -0700127 DISALLOW_COPY_AND_ASSIGN(Method);
128};
129
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700130struct TypedVarVector : public std::vector<NamedReference<Type>*> {
Yifan Hong7763ab32016-12-13 17:42:11 -0800131 TypedVarVector() = default;
132
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700133 bool add(NamedReference<Type>* v);
134
135 private:
Yifan Hong7763ab32016-12-13 17:42:11 -0800136 std::set<std::string> mNames;
137};
138
Andreas Huberc9410c72016-07-28 12:18:40 -0700139} // namespace android
140
141#endif // METHOD_H_
142