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