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