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