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 | |
Iliyan Malchev | a72e0d2 | 2016-09-09 11:03:08 -0700 | [diff] [blame] | 23 | #include <hidl-util/Formatter.h> |
| 24 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 25 | namespace android { |
| 26 | |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 27 | Method::Method(const char *name, |
| 28 | std::vector<TypedVar *> *args, |
| 29 | std::vector<TypedVar *> *results, |
Iliyan Malchev | 639bff8 | 2016-08-13 14:24:11 -0700 | [diff] [blame] | 30 | bool oneway, |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 31 | AnnotationVector *annotations) |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 32 | : mName(name), |
| 33 | mArgs(args), |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 34 | mResults(results), |
Iliyan Malchev | 639bff8 | 2016-08-13 14:24:11 -0700 | [diff] [blame] | 35 | mOneway(oneway), |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 36 | mAnnotationsByName(annotations) { |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 39 | std::string Method::name() const { |
| 40 | return mName; |
| 41 | } |
| 42 | |
| 43 | const std::vector<TypedVar *> &Method::args() const { |
| 44 | return *mArgs; |
| 45 | } |
| 46 | |
| 47 | const std::vector<TypedVar *> &Method::results() const { |
| 48 | return *mResults; |
| 49 | } |
| 50 | |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 51 | const AnnotationVector &Method::annotations() const { |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 52 | return *mAnnotationsByName; |
| 53 | } |
| 54 | |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 55 | void Method::generateCppSignature(Formatter &out, |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 56 | const std::string &className, |
| 57 | bool specifyNamespaces) const { |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 58 | const bool returnsValue = !results().empty(); |
| 59 | |
| 60 | const TypedVar *elidedReturn = canElideCallback(); |
| 61 | |
Iliyan Malchev | 7f949cb | 2016-09-09 13:16:19 -0700 | [diff] [blame] | 62 | std::string space = (specifyNamespaces ? "::android::hardware::" : ""); |
| 63 | |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 64 | if (elidedReturn == nullptr) { |
Iliyan Malchev | 7f949cb | 2016-09-09 13:16:19 -0700 | [diff] [blame] | 65 | out << space << "Return<void> "; |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 66 | } else { |
| 67 | std::string extra; |
Iliyan Malchev | 7f949cb | 2016-09-09 13:16:19 -0700 | [diff] [blame] | 68 | out << space |
| 69 | << "Return<" |
Steven Moreland | 941aea1 | 2016-09-09 14:20:22 -0700 | [diff] [blame] | 70 | << elidedReturn->type().getCppResultType(&extra, specifyNamespaces) |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 71 | << "> "; |
| 72 | } |
| 73 | |
| 74 | if (!className.empty()) { |
| 75 | out << className << "::"; |
| 76 | } |
| 77 | |
| 78 | out << name() |
| 79 | << "(" |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 80 | << GetArgSignature(args(), specifyNamespaces); |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 81 | |
| 82 | if (returnsValue && elidedReturn == nullptr) { |
| 83 | if (!args().empty()) { |
| 84 | out << ", "; |
| 85 | } |
| 86 | |
| 87 | out << name() << "_cb _hidl_cb"; |
| 88 | } |
| 89 | |
| 90 | out << ") "; |
| 91 | } |
| 92 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 93 | // static |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 94 | std::string Method::GetArgSignature(const std::vector<TypedVar *> &args, |
| 95 | bool specifyNamespaces) { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 96 | bool first = true; |
| 97 | std::string out; |
| 98 | for (const auto &arg : args) { |
| 99 | if (!first) { |
| 100 | out += ", "; |
| 101 | } |
| 102 | |
| 103 | std::string extra; |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 104 | out += arg->type().getCppArgumentType(&extra, |
| 105 | specifyNamespaces); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 106 | out += " "; |
| 107 | out += arg->name(); |
| 108 | out += extra; |
| 109 | |
| 110 | first = false; |
| 111 | } |
| 112 | |
| 113 | return out; |
| 114 | } |
| 115 | |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 116 | // static |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 117 | std::string Method::GetJavaArgSignature(const std::vector<TypedVar *> &args) { |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 118 | bool first = true; |
| 119 | std::string out; |
| 120 | for (const auto &arg : args) { |
| 121 | if (!first) { |
| 122 | out += ", "; |
| 123 | } |
| 124 | |
| 125 | std::string extra; |
| 126 | out += arg->type().getJavaType(); |
| 127 | out += " "; |
| 128 | out += arg->name(); |
| 129 | out += extra; |
| 130 | |
| 131 | first = false; |
| 132 | } |
| 133 | |
| 134 | return out; |
| 135 | } |
| 136 | |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 137 | void Method::dumpAnnotations(Formatter &out) const { |
| 138 | if (mAnnotationsByName->size() == 0) { |
| 139 | return; |
| 140 | } |
| 141 | |
| 142 | out << "// "; |
| 143 | for (size_t i = 0; i < mAnnotationsByName->size(); ++i) { |
| 144 | if (i > 0) { |
| 145 | out << " "; |
| 146 | } |
| 147 | mAnnotationsByName->valueAt(i)->dump(out); |
| 148 | } |
| 149 | out << "\n"; |
| 150 | } |
| 151 | |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 152 | bool Method::isJavaCompatible() const { |
| 153 | for (const auto &arg : *mArgs) { |
| 154 | if (!arg->isJavaCompatible()) { |
| 155 | return false; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | for (const auto &result : *mResults) { |
| 160 | if (!result->isJavaCompatible()) { |
| 161 | return false; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | return true; |
| 166 | } |
| 167 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 168 | const TypedVar* Method::canElideCallback() const { |
| 169 | auto &res = results(); |
| 170 | |
| 171 | // Can't elide callback for void or tuple-returning methods |
| 172 | if (res.size() != 1) { |
| 173 | return nullptr; |
| 174 | } |
| 175 | |
| 176 | const TypedVar *typedVar = res.at(0); |
| 177 | |
| 178 | // We only elide callbacks for methods returning a single scalar. |
| 179 | if (typedVar->type().resolveToScalarType() != nullptr) { |
| 180 | return typedVar; |
| 181 | } |
| 182 | |
| 183 | return nullptr; |
| 184 | } |
| 185 | |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 186 | //////////////////////////////////////////////////////////////////////////////// |
| 187 | |
| 188 | TypedVar::TypedVar(const char *name, Type *type) |
| 189 | : mName(name), |
| 190 | mType(type) { |
| 191 | } |
| 192 | |
| 193 | std::string TypedVar::name() const { |
| 194 | return mName; |
| 195 | } |
| 196 | |
| 197 | const Type &TypedVar::type() const { |
| 198 | return *mType; |
| 199 | } |
| 200 | |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 201 | bool TypedVar::isJavaCompatible() const { |
| 202 | return mType->isJavaCompatible(); |
| 203 | } |
| 204 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 205 | } // namespace android |
| 206 | |