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 | #include "Method.h" |
| 18 | |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 19 | #include "Annotation.h" |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 20 | #include "ScalarType.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 21 | #include "Type.h" |
| 22 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 23 | #include <android-base/logging.h> |
Iliyan Malchev | a72e0d2 | 2016-09-09 11:03:08 -0700 | [diff] [blame] | 24 | #include <hidl-util/Formatter.h> |
Timur Iskhakov | 7fa79f6 | 2017-08-09 11:04:54 -0700 | [diff] [blame] | 25 | #include <algorithm> |
Iliyan Malchev | a72e0d2 | 2016-09-09 11:03:08 -0700 | [diff] [blame] | 26 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 27 | namespace android { |
| 28 | |
Timur Iskhakov | 7fa79f6 | 2017-08-09 11:04:54 -0700 | [diff] [blame] | 29 | Method::Method(const char* name, std::vector<NamedReference<Type>*>* args, |
| 30 | std::vector<NamedReference<Type>*>* results, bool oneway, |
Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 31 | std::vector<Annotation*>* annotations, const Location& location) |
| 32 | : mName(name), |
| 33 | mArgs(args), |
| 34 | mResults(results), |
| 35 | mOneway(oneway), |
| 36 | mAnnotations(annotations), |
| 37 | mLocation(location) {} |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 38 | |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 39 | void Method::fillImplementation( |
| 40 | size_t serial, |
| 41 | MethodImpl cppImpl, |
| 42 | MethodImpl javaImpl) { |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 43 | mIsHidlReserved = true; |
| 44 | mSerial = serial; |
| 45 | mCppImpl = cppImpl; |
| 46 | mJavaImpl = javaImpl; |
Yifan Hong | cd2ae45 | 2017-01-31 14:33:40 -0800 | [diff] [blame] | 47 | |
| 48 | CHECK(mJavaImpl.find(IMPL_STUB_IMPL) == mJavaImpl.end()) |
Steven Moreland | 937408a | 2017-03-20 09:54:18 -0700 | [diff] [blame] | 49 | << "FATAL: mJavaImpl should not use IMPL_STUB_IMPL; use IMPL_INTERFACE instead."; |
Yifan Hong | cd2ae45 | 2017-01-31 14:33:40 -0800 | [diff] [blame] | 50 | CHECK(mCppImpl.find(IMPL_STUB_IMPL) == mCppImpl.end() || |
| 51 | mCppImpl.find(IMPL_STUB) == mCppImpl.end()) |
| 52 | << "FATAL: mCppImpl IMPL_STUB will override IMPL_STUB_IMPL."; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 55 | std::string Method::name() const { |
| 56 | return mName; |
| 57 | } |
| 58 | |
Timur Iskhakov | 7fa79f6 | 2017-08-09 11:04:54 -0700 | [diff] [blame] | 59 | const std::vector<NamedReference<Type>*>& Method::args() const { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 60 | return *mArgs; |
| 61 | } |
| 62 | |
Timur Iskhakov | 7fa79f6 | 2017-08-09 11:04:54 -0700 | [diff] [blame] | 63 | const std::vector<NamedReference<Type>*>& Method::results() const { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 64 | return *mResults; |
| 65 | } |
| 66 | |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 67 | const std::vector<Annotation *> &Method::annotations() const { |
| 68 | return *mAnnotations; |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Timur Iskhakov | 33431e6 | 2017-08-21 17:31:23 -0700 | [diff] [blame] | 71 | std::vector<Reference<Type>> Method::getReferences() const { |
| 72 | std::vector<Reference<Type>> ret; |
| 73 | for (const auto* arg : *mArgs) { |
| 74 | ret.push_back(*arg); |
| 75 | } |
| 76 | for (const auto* result : *mResults) { |
| 77 | ret.push_back(*result); |
| 78 | } |
| 79 | return ret; |
| 80 | } |
| 81 | |
Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 82 | status_t Method::evaluate() { |
Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 83 | for (auto* annotaion : *mAnnotations) { |
| 84 | status_t err = annotaion->evaluate(); |
| 85 | if (err != OK) return err; |
| 86 | } |
| 87 | |
| 88 | return OK; |
| 89 | } |
| 90 | |
| 91 | status_t Method::validate() const { |
Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 92 | for (const auto* annotaion : *mAnnotations) { |
| 93 | status_t err = annotaion->validate(); |
| 94 | if (err != OK) return err; |
| 95 | } |
| 96 | |
| 97 | return OK; |
| 98 | } |
| 99 | |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 100 | void Method::cppImpl(MethodImplType type, Formatter &out) const { |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 101 | CHECK(mIsHidlReserved); |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 102 | auto it = mCppImpl.find(type); |
| 103 | if (it != mCppImpl.end()) { |
Martijn Coenen | 8d12b50 | 2016-12-27 14:30:27 +0100 | [diff] [blame] | 104 | if (it->second != nullptr) { |
| 105 | it->second(out); |
| 106 | } |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 110 | void Method::javaImpl(MethodImplType type, Formatter &out) const { |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 111 | CHECK(mIsHidlReserved); |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 112 | auto it = mJavaImpl.find(type); |
| 113 | if (it != mJavaImpl.end()) { |
Martijn Coenen | 8d12b50 | 2016-12-27 14:30:27 +0100 | [diff] [blame] | 114 | if (it->second != nullptr) { |
| 115 | it->second(out); |
| 116 | } |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
Andreas Huber | 37065d6 | 2017-02-07 14:36:54 -0800 | [diff] [blame] | 120 | bool Method::isHiddenFromJava() const { |
| 121 | return isHidlReserved() && name() == "debug"; |
| 122 | } |
| 123 | |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 124 | bool Method::overridesCppImpl(MethodImplType type) const { |
| 125 | CHECK(mIsHidlReserved); |
| 126 | return mCppImpl.find(type) != mCppImpl.end(); |
| 127 | } |
| 128 | |
| 129 | bool Method::overridesJavaImpl(MethodImplType type) const { |
| 130 | CHECK(mIsHidlReserved); |
| 131 | return mJavaImpl.find(type) != mJavaImpl.end(); |
| 132 | } |
| 133 | |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 134 | Method *Method::copySignature() const { |
Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 135 | return new Method(mName.c_str(), mArgs, mResults, mOneway, mAnnotations, Location()); |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 136 | } |
| 137 | |
Steven Moreland | ef1a9fe | 2016-10-06 17:19:09 -0700 | [diff] [blame] | 138 | void Method::setSerialId(size_t serial) { |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 139 | CHECK(!mIsHidlReserved); |
Steven Moreland | ef1a9fe | 2016-10-06 17:19:09 -0700 | [diff] [blame] | 140 | mSerial = serial; |
| 141 | } |
| 142 | |
| 143 | size_t Method::getSerialId() const { |
| 144 | return mSerial; |
| 145 | } |
| 146 | |
Steven Moreland | d8b10ee | 2017-07-31 15:06:20 -0700 | [diff] [blame] | 147 | bool Method::hasEmptyCppArgSignature() const { |
| 148 | return args().empty() && (results().empty() || canElideCallback() != nullptr); |
| 149 | } |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 150 | |
Steven Moreland | d8b10ee | 2017-07-31 15:06:20 -0700 | [diff] [blame] | 151 | void Method::generateCppReturnType(Formatter &out, bool specifyNamespaces) const { |
Timur Iskhakov | 7fa79f6 | 2017-08-09 11:04:54 -0700 | [diff] [blame] | 152 | const NamedReference<Type>* elidedReturn = canElideCallback(); |
Steven Moreland | d8b10ee | 2017-07-31 15:06:20 -0700 | [diff] [blame] | 153 | const std::string space = (specifyNamespaces ? "::android::hardware::" : ""); |
Iliyan Malchev | 7f949cb | 2016-09-09 13:16:19 -0700 | [diff] [blame] | 154 | |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 155 | if (elidedReturn == nullptr) { |
Iliyan Malchev | 7f949cb | 2016-09-09 13:16:19 -0700 | [diff] [blame] | 156 | out << space << "Return<void> "; |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 157 | } else { |
Iliyan Malchev | 7f949cb | 2016-09-09 13:16:19 -0700 | [diff] [blame] | 158 | out << space |
| 159 | << "Return<" |
Yifan Hong | 3b320f8 | 2016-11-01 15:15:54 -0700 | [diff] [blame] | 160 | << elidedReturn->type().getCppResultType( specifyNamespaces) |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 161 | << "> "; |
| 162 | } |
Steven Moreland | d8b10ee | 2017-07-31 15:06:20 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | void Method::generateCppSignature(Formatter &out, |
| 166 | const std::string &className, |
| 167 | bool specifyNamespaces) const { |
| 168 | generateCppReturnType(out, specifyNamespaces); |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 169 | |
| 170 | if (!className.empty()) { |
| 171 | out << className << "::"; |
| 172 | } |
| 173 | |
| 174 | out << name() |
Yifan Hong | 932464e | 2017-03-30 15:40:22 -0700 | [diff] [blame] | 175 | << "("; |
| 176 | emitCppArgSignature(out, specifyNamespaces); |
Steven Moreland | 41c6d2e | 2016-11-07 12:26:54 -0800 | [diff] [blame] | 177 | out << ")"; |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Timur Iskhakov | 7fa79f6 | 2017-08-09 11:04:54 -0700 | [diff] [blame] | 180 | static void emitCppArgResultSignature(Formatter& out, |
| 181 | const std::vector<NamedReference<Type>*>& args, |
| 182 | bool specifyNamespaces) { |
Yifan Hong | 932464e | 2017-03-30 15:40:22 -0700 | [diff] [blame] | 183 | out.join(args.begin(), args.end(), ", ", [&](auto arg) { |
| 184 | out << arg->type().getCppArgumentType(specifyNamespaces); |
| 185 | out << " "; |
| 186 | out << arg->name(); |
| 187 | }); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Timur Iskhakov | 7fa79f6 | 2017-08-09 11:04:54 -0700 | [diff] [blame] | 190 | static void emitJavaArgResultSignature(Formatter& out, |
| 191 | const std::vector<NamedReference<Type>*>& args) { |
Yifan Hong | 932464e | 2017-03-30 15:40:22 -0700 | [diff] [blame] | 192 | out.join(args.begin(), args.end(), ", ", [&](auto arg) { |
| 193 | out << arg->type().getJavaType(); |
| 194 | out << " "; |
| 195 | out << arg->name(); |
| 196 | }); |
| 197 | } |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 198 | |
Yifan Hong | 932464e | 2017-03-30 15:40:22 -0700 | [diff] [blame] | 199 | void Method::emitCppArgSignature(Formatter &out, bool specifyNamespaces) const { |
| 200 | emitCppArgResultSignature(out, args(), specifyNamespaces); |
Steven Moreland | d8b10ee | 2017-07-31 15:06:20 -0700 | [diff] [blame] | 201 | |
| 202 | const bool returnsValue = !results().empty(); |
Timur Iskhakov | 7fa79f6 | 2017-08-09 11:04:54 -0700 | [diff] [blame] | 203 | const NamedReference<Type>* elidedReturn = canElideCallback(); |
Steven Moreland | d8b10ee | 2017-07-31 15:06:20 -0700 | [diff] [blame] | 204 | if (returnsValue && elidedReturn == nullptr) { |
| 205 | if (!args().empty()) { |
| 206 | out << ", "; |
| 207 | } |
| 208 | |
| 209 | out << name() << "_cb _hidl_cb"; |
| 210 | } |
Yifan Hong | 932464e | 2017-03-30 15:40:22 -0700 | [diff] [blame] | 211 | } |
| 212 | void Method::emitCppResultSignature(Formatter &out, bool specifyNamespaces) const { |
| 213 | emitCppArgResultSignature(out, results(), specifyNamespaces); |
| 214 | } |
| 215 | void Method::emitJavaArgSignature(Formatter &out) const { |
| 216 | emitJavaArgResultSignature(out, args()); |
| 217 | } |
| 218 | void Method::emitJavaResultSignature(Formatter &out) const { |
| 219 | emitJavaArgResultSignature(out, results()); |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 222 | void Method::dumpAnnotations(Formatter &out) const { |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 223 | if (mAnnotations->size() == 0) { |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 224 | return; |
| 225 | } |
| 226 | |
| 227 | out << "// "; |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 228 | for (size_t i = 0; i < mAnnotations->size(); ++i) { |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 229 | if (i > 0) { |
| 230 | out << " "; |
| 231 | } |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 232 | mAnnotations->at(i)->dump(out); |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 233 | } |
| 234 | out << "\n"; |
| 235 | } |
| 236 | |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 237 | bool Method::isJavaCompatible() const { |
Andreas Huber | 37065d6 | 2017-02-07 14:36:54 -0800 | [diff] [blame] | 238 | if (isHiddenFromJava()) { |
| 239 | return true; |
| 240 | } |
| 241 | |
Timur Iskhakov | 7fa79f6 | 2017-08-09 11:04:54 -0700 | [diff] [blame] | 242 | if (!std::all_of(mArgs->begin(), mArgs->end(), |
| 243 | [](const auto* arg) { return (*arg)->isJavaCompatible(); })) { |
| 244 | return false; |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Timur Iskhakov | 7fa79f6 | 2017-08-09 11:04:54 -0700 | [diff] [blame] | 247 | if (!std::all_of(mResults->begin(), mResults->end(), |
| 248 | [](const auto* arg) { return (*arg)->isJavaCompatible(); })) { |
| 249 | return false; |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | return true; |
| 253 | } |
| 254 | |
Timur Iskhakov | 7fa79f6 | 2017-08-09 11:04:54 -0700 | [diff] [blame] | 255 | const NamedReference<Type>* Method::canElideCallback() const { |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 256 | // Can't elide callback for void or tuple-returning methods |
Steven Moreland | 9df5244 | 2016-12-12 08:51:14 -0800 | [diff] [blame] | 257 | if (mResults->size() != 1) { |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 258 | return nullptr; |
| 259 | } |
| 260 | |
Timur Iskhakov | 7fa79f6 | 2017-08-09 11:04:54 -0700 | [diff] [blame] | 261 | const NamedReference<Type>* typedVar = mResults->at(0); |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 262 | |
Steven Moreland | 9df5244 | 2016-12-12 08:51:14 -0800 | [diff] [blame] | 263 | if (typedVar->type().isElidableType()) { |
Martijn Coenen | 99e6beb | 2016-12-01 15:48:42 +0100 | [diff] [blame] | 264 | return typedVar; |
| 265 | } |
| 266 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 267 | return nullptr; |
| 268 | } |
| 269 | |
Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 270 | const Location& Method::location() const { |
| 271 | return mLocation; |
| 272 | } |
| 273 | |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 274 | //////////////////////////////////////////////////////////////////////////////// |
| 275 | |
Timur Iskhakov | 7fa79f6 | 2017-08-09 11:04:54 -0700 | [diff] [blame] | 276 | bool TypedVarVector::add(NamedReference<Type>* v) { |
Yifan Hong | 7763ab3 | 2016-12-13 17:42:11 -0800 | [diff] [blame] | 277 | if (mNames.emplace(v->name()).second) { |
| 278 | push_back(v); |
| 279 | return true; |
| 280 | } |
| 281 | return false; |
| 282 | } |
| 283 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 284 | } // namespace android |
| 285 | |