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 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 74 | void Method::cppImpl(Formatter &out) const { |
| 75 | CHECK(mIsHidlReserved); |
| 76 | if (mCppImpl) { |
| 77 | mCppImpl(out); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | void Method::javaImpl(Formatter &out) const { |
| 82 | CHECK(mIsHidlReserved); |
| 83 | if (mJavaImpl) { |
| 84 | mJavaImpl(out); |
| 85 | } |
| 86 | } |
| 87 | |
Steven Moreland | ef1a9fe | 2016-10-06 17:19:09 -0700 | [diff] [blame] | 88 | void Method::setSerialId(size_t serial) { |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 89 | CHECK(!mIsHidlReserved); |
Steven Moreland | ef1a9fe | 2016-10-06 17:19:09 -0700 | [diff] [blame] | 90 | mSerial = serial; |
| 91 | } |
| 92 | |
| 93 | size_t Method::getSerialId() const { |
| 94 | return mSerial; |
| 95 | } |
| 96 | |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 97 | void Method::generateCppSignature(Formatter &out, |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 98 | const std::string &className, |
| 99 | bool specifyNamespaces) const { |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 100 | const bool returnsValue = !results().empty(); |
| 101 | |
| 102 | const TypedVar *elidedReturn = canElideCallback(); |
| 103 | |
Iliyan Malchev | 7f949cb | 2016-09-09 13:16:19 -0700 | [diff] [blame] | 104 | std::string space = (specifyNamespaces ? "::android::hardware::" : ""); |
| 105 | |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 106 | if (elidedReturn == nullptr) { |
Iliyan Malchev | 7f949cb | 2016-09-09 13:16:19 -0700 | [diff] [blame] | 107 | out << space << "Return<void> "; |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 108 | } else { |
Iliyan Malchev | 7f949cb | 2016-09-09 13:16:19 -0700 | [diff] [blame] | 109 | out << space |
| 110 | << "Return<" |
Yifan Hong | 3b320f8 | 2016-11-01 15:15:54 -0700 | [diff] [blame] | 111 | << elidedReturn->type().getCppResultType( specifyNamespaces) |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 112 | << "> "; |
| 113 | } |
| 114 | |
| 115 | if (!className.empty()) { |
| 116 | out << className << "::"; |
| 117 | } |
| 118 | |
| 119 | out << name() |
| 120 | << "(" |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 121 | << GetArgSignature(args(), specifyNamespaces); |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 122 | |
| 123 | if (returnsValue && elidedReturn == nullptr) { |
| 124 | if (!args().empty()) { |
| 125 | out << ", "; |
| 126 | } |
| 127 | |
| 128 | out << name() << "_cb _hidl_cb"; |
| 129 | } |
| 130 | |
Steven Moreland | 41c6d2e | 2016-11-07 12:26:54 -0800 | [diff] [blame] | 131 | out << ")"; |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 134 | // static |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 135 | std::string Method::GetArgSignature(const std::vector<TypedVar *> &args, |
| 136 | bool specifyNamespaces) { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 137 | bool first = true; |
| 138 | std::string out; |
| 139 | for (const auto &arg : args) { |
| 140 | if (!first) { |
| 141 | out += ", "; |
| 142 | } |
| 143 | |
Yifan Hong | 3b320f8 | 2016-11-01 15:15:54 -0700 | [diff] [blame] | 144 | out += arg->type().getCppArgumentType(specifyNamespaces); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 145 | out += " "; |
| 146 | out += arg->name(); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 147 | |
| 148 | first = false; |
| 149 | } |
| 150 | |
| 151 | return out; |
| 152 | } |
| 153 | |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 154 | // static |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 155 | std::string Method::GetJavaArgSignature(const std::vector<TypedVar *> &args) { |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 156 | bool first = true; |
| 157 | std::string out; |
| 158 | for (const auto &arg : args) { |
| 159 | if (!first) { |
| 160 | out += ", "; |
| 161 | } |
| 162 | |
Yifan Hong | 4ed1347 | 2016-11-02 10:44:11 -0700 | [diff] [blame] | 163 | out += arg->type().getJavaType(); |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 164 | out += " "; |
| 165 | out += arg->name(); |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 166 | |
| 167 | first = false; |
| 168 | } |
| 169 | |
| 170 | return out; |
| 171 | } |
| 172 | |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 173 | void Method::dumpAnnotations(Formatter &out) const { |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 174 | if (mAnnotations->size() == 0) { |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 175 | return; |
| 176 | } |
| 177 | |
| 178 | out << "// "; |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 179 | for (size_t i = 0; i < mAnnotations->size(); ++i) { |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 180 | if (i > 0) { |
| 181 | out << " "; |
| 182 | } |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 183 | mAnnotations->at(i)->dump(out); |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 184 | } |
| 185 | out << "\n"; |
| 186 | } |
| 187 | |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 188 | bool Method::isJavaCompatible() const { |
| 189 | for (const auto &arg : *mArgs) { |
| 190 | if (!arg->isJavaCompatible()) { |
| 191 | return false; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | for (const auto &result : *mResults) { |
| 196 | if (!result->isJavaCompatible()) { |
| 197 | return false; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | return true; |
| 202 | } |
| 203 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 204 | const TypedVar* Method::canElideCallback() const { |
| 205 | auto &res = results(); |
| 206 | |
| 207 | // Can't elide callback for void or tuple-returning methods |
| 208 | if (res.size() != 1) { |
| 209 | return nullptr; |
| 210 | } |
| 211 | |
| 212 | const TypedVar *typedVar = res.at(0); |
| 213 | |
| 214 | // We only elide callbacks for methods returning a single scalar. |
| 215 | if (typedVar->type().resolveToScalarType() != nullptr) { |
| 216 | return typedVar; |
| 217 | } |
| 218 | |
| 219 | return nullptr; |
| 220 | } |
| 221 | |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 222 | //////////////////////////////////////////////////////////////////////////////// |
| 223 | |
| 224 | TypedVar::TypedVar(const char *name, Type *type) |
| 225 | : mName(name), |
| 226 | mType(type) { |
| 227 | } |
| 228 | |
| 229 | std::string TypedVar::name() const { |
| 230 | return mName; |
| 231 | } |
| 232 | |
| 233 | const Type &TypedVar::type() const { |
| 234 | return *mType; |
| 235 | } |
| 236 | |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 237 | bool TypedVar::isJavaCompatible() const { |
| 238 | return mType->isJavaCompatible(); |
| 239 | } |
| 240 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 241 | } // namespace android |
| 242 | |