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