blob: 941f7b7fdfc518b59a247ec5ff9802d0010e2faa [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>
Andreas Huber881227d2016-08-02 14:20:21 -070028#include <vector>
Andreas Huberc9410c72016-07-28 12:18:40 -070029
Timur Iskhakovcec46c42017-08-09 00:22:02 -070030#include "Location.h"
Timur Iskhakov505316c2017-08-05 03:38:59 +000031#include "Reference.h"
32
Andreas Huberc9410c72016-07-28 12:18:40 -070033namespace android {
34
Andreas Huber3599d922016-08-09 10:42:57 -070035struct Annotation;
Andreas Huberc9410c72016-07-28 12:18:40 -070036struct Formatter;
Iliyan Malchev40d474a2016-08-16 06:20:17 -070037struct ScalarType;
Andreas Huberc9410c72016-07-28 12:18:40 -070038struct Type;
Yifan Hong7763ab32016-12-13 17:42:11 -080039struct TypedVarVector;
Andreas Huberc9410c72016-07-28 12:18:40 -070040
Martijn Coenen115d4282016-12-19 05:14:04 +010041enum MethodImplType {
Steven Moreland937408a2017-03-20 09:54:18 -070042 IMPL_INTERFACE,
Martijn Coenen115d4282016-12-19 05:14:04 +010043 IMPL_PROXY,
Yifan Hongcd2ae452017-01-31 14:33:40 -080044 IMPL_STUB, // overrides the code in onTransact; IMPL_STUB_IMPL will be ignored
45 IMPL_STUB_IMPL, // use this->method() instead of mImpl->method()
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -080046 IMPL_PASSTHROUGH,
Martijn Coenen115d4282016-12-19 05:14:04 +010047};
48
49using MethodImpl = std::map<MethodImplType, std::function<void(Formatter &)>>;
Yifan Hong10fe0b52016-10-19 14:20:17 -070050
Andreas Huberc9410c72016-07-28 12:18:40 -070051struct Method {
Timur Iskhakov7fa79f62017-08-09 11:04:54 -070052 Method(const char* name, std::vector<NamedReference<Type>*>* args,
53 std::vector<NamedReference<Type>*>* results, bool oneway,
Timur Iskhakovcec46c42017-08-09 00:22:02 -070054 std::vector<Annotation*>* annotations, const Location& location);
Andreas Huberc9410c72016-07-28 12:18:40 -070055
Andreas Huber881227d2016-08-02 14:20:21 -070056 std::string name() const;
Timur Iskhakov7fa79f62017-08-09 11:04:54 -070057 const std::vector<NamedReference<Type>*>& args() const;
58 const std::vector<NamedReference<Type>*>& results() const;
Iliyan Malchev639bff82016-08-13 14:24:11 -070059 bool isOneway() const { return mOneway; }
Martijn Coenen115d4282016-12-19 05:14:04 +010060 bool overridesCppImpl(MethodImplType type) const;
61 bool overridesJavaImpl(MethodImplType type) const;
62 void cppImpl(MethodImplType type, Formatter &out) const;
63 void javaImpl(MethodImplType type, Formatter &out) const;
Yifan Hong10fe0b52016-10-19 14:20:17 -070064 bool isHidlReserved() const { return mIsHidlReserved; }
Andreas Huber37065d62017-02-07 14:36:54 -080065 bool isHiddenFromJava() const;
Steven Morelandd537ab02016-09-12 10:32:01 -070066 const std::vector<Annotation *> &annotations() const;
Andreas Huber881227d2016-08-02 14:20:21 -070067
Timur Iskhakov33431e62017-08-21 17:31:23 -070068 std::vector<Reference<Type>> getReferences() const;
69
Timur Iskhakovcec46c42017-08-09 00:22:02 -070070 status_t evaluate();
71 status_t validate() const;
72
Yifan Hongffa91392017-01-31 13:41:23 -080073 // Make a copy with the same name, args, results, oneway, annotations.
74 // Implementations, serial are not copied.
75 Method *copySignature() const;
76
Steven Morelandef1a9fe2016-10-06 17:19:09 -070077 void setSerialId(size_t serial);
78 size_t getSerialId() const;
79
Yifan Hongffa91392017-01-31 13:41:23 -080080 // 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
Steven Morelandd8b10ee2017-07-31 15:06:20 -070087 void generateCppReturnType(Formatter &out, bool specifyNamespaces = true) const;
Steven Morelanda7a421a2016-09-07 08:35:18 -070088 void generateCppSignature(Formatter &out,
Yifan Hong068c5522016-10-31 14:07:25 -070089 const std::string &className = "",
90 bool specifyNamespaces = true) const;
Steven Morelanda7a421a2016-09-07 08:35:18 -070091
Steven Morelandd8b10ee2017-07-31 15:06:20 -070092 bool hasEmptyCppArgSignature() const;
93 void emitCppArgSignature(Formatter &out, bool specifyNamespaces = true) const;
94 void emitCppResultSignature(Formatter &out, bool specifyNamespaces = true) const;
95
Yifan Hong932464e2017-03-30 15:40:22 -070096 void emitJavaArgSignature(Formatter &out) const;
97 void emitJavaResultSignature(Formatter &out) const;
Andreas Huber881227d2016-08-02 14:20:21 -070098
Timur Iskhakov7fa79f62017-08-09 11:04:54 -070099 const NamedReference<Type>* canElideCallback() const;
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700100
Andreas Huber3599d922016-08-09 10:42:57 -0700101 void dumpAnnotations(Formatter &out) const;
102
Andreas Huber70a59e12016-08-16 12:57:01 -0700103 bool isJavaCompatible() const;
104
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700105 const Location& location() const;
106
107 private:
Andreas Huberc9410c72016-07-28 12:18:40 -0700108 std::string mName;
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700109 size_t mSerial = 0;
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700110 std::vector<NamedReference<Type>*>* mArgs;
111 std::vector<NamedReference<Type>*>* mResults;
Iliyan Malchev639bff82016-08-13 14:24:11 -0700112 bool mOneway;
Steven Morelandd537ab02016-09-12 10:32:01 -0700113 std::vector<Annotation *> *mAnnotations;
Andreas Huberc9410c72016-07-28 12:18:40 -0700114
Yifan Hong10fe0b52016-10-19 14:20:17 -0700115 bool mIsHidlReserved = false;
Martijn Coenen115d4282016-12-19 05:14:04 +0100116 // The following fields have no meaning if mIsHidlReserved is false.
Yifan Hong10fe0b52016-10-19 14:20:17 -0700117 // hard-coded implementation for HIDL reserved methods.
Martijn Coenen115d4282016-12-19 05:14:04 +0100118 MethodImpl mCppImpl;
119 MethodImpl mJavaImpl;
Yifan Hong10fe0b52016-10-19 14:20:17 -0700120
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700121 const Location& mLocation;
122
Andreas Huberc9410c72016-07-28 12:18:40 -0700123 DISALLOW_COPY_AND_ASSIGN(Method);
124};
125
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700126struct TypedVarVector : public std::vector<NamedReference<Type>*> {
Yifan Hong7763ab32016-12-13 17:42:11 -0800127 TypedVarVector() = default;
128
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700129 bool add(NamedReference<Type>* v);
130
131 private:
Yifan Hong7763ab32016-12-13 17:42:11 -0800132 std::set<std::string> mNames;
133};
134
Andreas Huberc9410c72016-07-28 12:18:40 -0700135} // namespace android
136
137#endif // METHOD_H_
138