Andreas Huber | 1aec397 | 2016-08-26 09:26:32 -0700 | [diff] [blame] | 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 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 17 | #ifndef METHOD_H_ |
| 18 | |
| 19 | #define METHOD_H_ |
| 20 | |
| 21 | #include <android-base/macros.h> |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 22 | #include <hidl-util/Formatter.h> |
Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 23 | #include <utils/Errors.h> |
| 24 | #include <functional> |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 25 | #include <map> |
Yifan Hong | 7763ab3 | 2016-12-13 17:42:11 -0800 | [diff] [blame] | 26 | #include <set> |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 27 | #include <string> |
Timur Iskhakov | 5dc72fe | 2017-09-07 23:13:44 -0700 | [diff] [blame] | 28 | #include <unordered_set> |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 29 | #include <vector> |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 30 | |
Steven Moreland | 073269e | 2018-05-17 15:45:26 -0700 | [diff] [blame] | 31 | #include "DocComment.h" |
Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 32 | #include "Location.h" |
Timur Iskhakov | 505316c | 2017-08-05 03:38:59 +0000 | [diff] [blame] | 33 | #include "Reference.h" |
| 34 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 35 | namespace android { |
| 36 | |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 37 | struct Annotation; |
Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 38 | struct ConstantExpression; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 39 | struct Formatter; |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 40 | struct ScalarType; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 41 | struct Type; |
Yifan Hong | 7763ab3 | 2016-12-13 17:42:11 -0800 | [diff] [blame] | 42 | struct TypedVarVector; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 43 | |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 44 | enum MethodImplType { |
Steven Moreland | 937408a | 2017-03-20 09:54:18 -0700 | [diff] [blame] | 45 | IMPL_INTERFACE, |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 46 | IMPL_PROXY, |
Yifan Hong | cd2ae45 | 2017-01-31 14:33:40 -0800 | [diff] [blame] | 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() |
Zhuoyao Zhang | dd85c5c | 2017-01-03 17:30:24 -0800 | [diff] [blame] | 49 | IMPL_PASSTHROUGH, |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | using MethodImpl = std::map<MethodImplType, std::function<void(Formatter &)>>; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 53 | |
Steven Moreland | 073269e | 2018-05-17 15:45:26 -0700 | [diff] [blame] | 54 | struct Method : DocCommentable { |
Timur Iskhakov | 7fa79f6 | 2017-08-09 11:04:54 -0700 | [diff] [blame] | 55 | Method(const char* name, std::vector<NamedReference<Type>*>* args, |
| 56 | std::vector<NamedReference<Type>*>* results, bool oneway, |
Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 57 | std::vector<Annotation*>* annotations, const Location& location); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 58 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 59 | std::string name() const; |
Timur Iskhakov | 7fa79f6 | 2017-08-09 11:04:54 -0700 | [diff] [blame] | 60 | const std::vector<NamedReference<Type>*>& args() const; |
| 61 | const std::vector<NamedReference<Type>*>& results() const; |
Iliyan Malchev | 639bff8 | 2016-08-13 14:24:11 -0700 | [diff] [blame] | 62 | bool isOneway() const { return mOneway; } |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 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; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 67 | bool isHidlReserved() const { return mIsHidlReserved; } |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 68 | const std::vector<Annotation *> &annotations() const; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 69 | |
Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 70 | std::vector<Reference<Type>*> getReferences(); |
| 71 | std::vector<const Reference<Type>*> getReferences() const; |
Timur Iskhakov | 33431e6 | 2017-08-21 17:31:23 -0700 | [diff] [blame] | 72 | |
Timur Iskhakov | ff5e64a | 2017-09-11 14:56:18 -0700 | [diff] [blame] | 73 | std::vector<Reference<Type>*> getStrongReferences(); |
| 74 | std::vector<const Reference<Type>*> getStrongReferences() const; |
| 75 | |
Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 76 | std::vector<ConstantExpression*> getConstantExpressions(); |
| 77 | std::vector<const ConstantExpression*> getConstantExpressions() const; |
Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 78 | |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 79 | // Make a copy with the same name, args, results, oneway, annotations. |
| 80 | // Implementations, serial are not copied. |
| 81 | Method *copySignature() const; |
| 82 | |
Steven Moreland | ef1a9fe | 2016-10-06 17:19:09 -0700 | [diff] [blame] | 83 | void setSerialId(size_t serial); |
| 84 | size_t getSerialId() const; |
| 85 | |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 86 | // Fill implementation for HIDL reserved methods. mIsHidlReserved will be |
| 87 | // set to true. |
| 88 | void fillImplementation( |
| 89 | size_t serial, |
| 90 | MethodImpl cppImpl, |
| 91 | MethodImpl javaImpl); |
| 92 | |
Steven Moreland | d8b10ee | 2017-07-31 15:06:20 -0700 | [diff] [blame] | 93 | void generateCppReturnType(Formatter &out, bool specifyNamespaces = true) const; |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 94 | void generateCppSignature(Formatter &out, |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 95 | const std::string &className = "", |
| 96 | bool specifyNamespaces = true) const; |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 97 | |
Steven Moreland | d8b10ee | 2017-07-31 15:06:20 -0700 | [diff] [blame] | 98 | bool hasEmptyCppArgSignature() const; |
| 99 | void emitCppArgSignature(Formatter &out, bool specifyNamespaces = true) const; |
| 100 | void emitCppResultSignature(Formatter &out, bool specifyNamespaces = true) const; |
| 101 | |
Yifan Hong | 932464e | 2017-03-30 15:40:22 -0700 | [diff] [blame] | 102 | void emitJavaArgSignature(Formatter &out) const; |
| 103 | void emitJavaResultSignature(Formatter &out) const; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 104 | |
Timur Iskhakov | 7fa79f6 | 2017-08-09 11:04:54 -0700 | [diff] [blame] | 105 | const NamedReference<Type>* canElideCallback() const; |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 106 | |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 107 | void dumpAnnotations(Formatter &out) const; |
| 108 | |
Timur Iskhakov | 5dc72fe | 2017-09-07 23:13:44 -0700 | [diff] [blame] | 109 | bool deepIsJavaCompatible(std::unordered_set<const Type*>* visited) const; |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 110 | |
Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 111 | const Location& location() const; |
| 112 | |
| 113 | private: |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 114 | std::string mName; |
Steven Moreland | ef1a9fe | 2016-10-06 17:19:09 -0700 | [diff] [blame] | 115 | size_t mSerial = 0; |
Timur Iskhakov | 7fa79f6 | 2017-08-09 11:04:54 -0700 | [diff] [blame] | 116 | std::vector<NamedReference<Type>*>* mArgs; |
| 117 | std::vector<NamedReference<Type>*>* mResults; |
Iliyan Malchev | 639bff8 | 2016-08-13 14:24:11 -0700 | [diff] [blame] | 118 | bool mOneway; |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 119 | std::vector<Annotation *> *mAnnotations; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 120 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 121 | bool mIsHidlReserved = false; |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 122 | // The following fields have no meaning if mIsHidlReserved is false. |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 123 | // hard-coded implementation for HIDL reserved methods. |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 124 | MethodImpl mCppImpl; |
| 125 | MethodImpl mJavaImpl; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 126 | |
Timur Iskhakov | 532654e | 2017-09-07 14:03:18 -0700 | [diff] [blame] | 127 | const Location mLocation; |
Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 128 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 129 | DISALLOW_COPY_AND_ASSIGN(Method); |
| 130 | }; |
| 131 | |
Timur Iskhakov | 7fa79f6 | 2017-08-09 11:04:54 -0700 | [diff] [blame] | 132 | struct TypedVarVector : public std::vector<NamedReference<Type>*> { |
Yifan Hong | 7763ab3 | 2016-12-13 17:42:11 -0800 | [diff] [blame] | 133 | TypedVarVector() = default; |
| 134 | |
Timur Iskhakov | 7fa79f6 | 2017-08-09 11:04:54 -0700 | [diff] [blame] | 135 | bool add(NamedReference<Type>* v); |
| 136 | |
| 137 | private: |
Yifan Hong | 7763ab3 | 2016-12-13 17:42:11 -0800 | [diff] [blame] | 138 | std::set<std::string> mNames; |
| 139 | }; |
| 140 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 141 | } // namespace android |
| 142 | |
| 143 | #endif // METHOD_H_ |
| 144 | |