1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
|
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "Method.h"
#include "Annotation.h"
#include "ConstantExpression.h"
#include "ScalarType.h"
#include "Type.h"
#include <android-base/logging.h>
#include <hidl-util/Formatter.h>
#include <algorithm>
namespace android {
Method::Method(const char* name, std::vector<NamedReference<Type>*>* args,
std::vector<NamedReference<Type>*>* results, bool oneway,
std::vector<Annotation*>* annotations, const Location& location)
: mName(name),
mArgs(args),
mResults(results),
mOneway(oneway),
mAnnotations(annotations),
mLocation(location) {}
void Method::fillImplementation(
size_t serial,
MethodImpl cppImpl,
MethodImpl javaImpl) {
mIsHidlReserved = true;
mSerial = serial;
mCppImpl = cppImpl;
mJavaImpl = javaImpl;
CHECK(mJavaImpl.find(IMPL_STUB_IMPL) == mJavaImpl.end())
<< "FATAL: mJavaImpl should not use IMPL_STUB_IMPL; use IMPL_INTERFACE instead.";
CHECK(mCppImpl.find(IMPL_STUB_IMPL) == mCppImpl.end() ||
mCppImpl.find(IMPL_STUB) == mCppImpl.end())
<< "FATAL: mCppImpl IMPL_STUB will override IMPL_STUB_IMPL.";
}
std::string Method::name() const {
return mName;
}
const std::vector<NamedReference<Type>*>& Method::args() const {
return *mArgs;
}
const std::vector<NamedReference<Type>*>& Method::results() const {
return *mResults;
}
const std::vector<Annotation *> &Method::annotations() const {
return *mAnnotations;
}
std::vector<Reference<Type>*> Method::getReferences() {
const auto& constRet = static_cast<const Method*>(this)->getReferences();
std::vector<Reference<Type>*> ret(constRet.size());
std::transform(constRet.begin(), constRet.end(), ret.begin(),
[](const auto* ref) { return const_cast<Reference<Type>*>(ref); });
return ret;
}
std::vector<const Reference<Type>*> Method::getReferences() const {
std::vector<const Reference<Type>*> ret;
ret.insert(ret.end(), mArgs->begin(), mArgs->end());
ret.insert(ret.end(), mResults->begin(), mResults->end());
return ret;
}
std::vector<Reference<Type>*> Method::getStrongReferences() {
const auto& constRet = static_cast<const Method*>(this)->getStrongReferences();
std::vector<Reference<Type>*> ret(constRet.size());
std::transform(constRet.begin(), constRet.end(), ret.begin(),
[](const auto* ref) { return const_cast<Reference<Type>*>(ref); });
return ret;
}
std::vector<const Reference<Type>*> Method::getStrongReferences() const {
std::vector<const Reference<Type>*> ret;
for (const auto* ref : getReferences()) {
if (!ref->shallowGet()->isNeverStrongReference()) {
ret.push_back(ref);
}
}
return ret;
}
std::vector<ConstantExpression*> Method::getConstantExpressions() {
const auto& constRet = static_cast<const Method*>(this)->getConstantExpressions();
std::vector<ConstantExpression*> ret(constRet.size());
std::transform(constRet.begin(), constRet.end(), ret.begin(),
[](const auto* ce) { return const_cast<ConstantExpression*>(ce); });
return ret;
}
std::vector<const ConstantExpression*> Method::getConstantExpressions() const {
std::vector<const ConstantExpression*> ret;
for (const auto* annotation : *mAnnotations) {
const auto& retAnnotation = annotation->getConstantExpressions();
ret.insert(ret.end(), retAnnotation.begin(), retAnnotation.end());
}
return ret;
}
void Method::cppImpl(MethodImplType type, Formatter &out) const {
CHECK(mIsHidlReserved);
auto it = mCppImpl.find(type);
if (it != mCppImpl.end()) {
if (it->second != nullptr) {
it->second(out);
}
}
}
void Method::javaImpl(MethodImplType type, Formatter &out) const {
CHECK(mIsHidlReserved);
auto it = mJavaImpl.find(type);
if (it != mJavaImpl.end()) {
if (it->second != nullptr) {
it->second(out);
}
}
}
bool Method::overridesCppImpl(MethodImplType type) const {
CHECK(mIsHidlReserved);
return mCppImpl.find(type) != mCppImpl.end();
}
bool Method::overridesJavaImpl(MethodImplType type) const {
CHECK(mIsHidlReserved);
return mJavaImpl.find(type) != mJavaImpl.end();
}
Method* Method::copySignature() const {
Method* method = new Method(mName.c_str(), mArgs, mResults, mOneway, mAnnotations, location());
method->setDocComment(getDocComment());
return method;
}
void Method::setSerialId(size_t serial) {
CHECK(!mIsHidlReserved);
mSerial = serial;
}
size_t Method::getSerialId() const {
return mSerial;
}
bool Method::hasEmptyCppArgSignature() const {
return args().empty() && (results().empty() || canElideCallback() != nullptr);
}
void Method::generateCppReturnType(Formatter &out, bool specifyNamespaces) const {
const NamedReference<Type>* elidedReturn = canElideCallback();
const std::string space = (specifyNamespaces ? "::android::hardware::" : "");
if (elidedReturn == nullptr) {
out << space << "Return<void> ";
} else {
out << space
<< "Return<"
<< elidedReturn->type().getCppResultType( specifyNamespaces)
<< "> ";
}
}
void Method::generateCppSignature(Formatter &out,
const std::string &className,
bool specifyNamespaces) const {
generateCppReturnType(out, specifyNamespaces);
if (!className.empty()) {
out << className << "::";
}
out << name()
<< "(";
emitCppArgSignature(out, specifyNamespaces);
out << ")";
}
static void emitCppArgResultSignature(Formatter& out,
const std::vector<NamedReference<Type>*>& args,
bool specifyNamespaces) {
out.join(args.begin(), args.end(), ", ", [&](auto arg) {
out << arg->type().getCppArgumentType(specifyNamespaces);
out << " ";
out << arg->name();
});
}
static void emitJavaArgResultSignature(Formatter& out,
const std::vector<NamedReference<Type>*>& args) {
out.join(args.begin(), args.end(), ", ", [&](auto arg) {
out << arg->type().getJavaType();
out << " ";
out << arg->name();
});
}
void Method::emitCppArgSignature(Formatter &out, bool specifyNamespaces) const {
emitCppArgResultSignature(out, args(), specifyNamespaces);
const bool returnsValue = !results().empty();
const NamedReference<Type>* elidedReturn = canElideCallback();
if (returnsValue && elidedReturn == nullptr) {
if (!args().empty()) {
out << ", ";
}
out << name() << "_cb _hidl_cb";
}
}
void Method::emitCppResultSignature(Formatter &out, bool specifyNamespaces) const {
emitCppArgResultSignature(out, results(), specifyNamespaces);
}
void Method::emitJavaArgSignature(Formatter &out) const {
emitJavaArgResultSignature(out, args());
}
void Method::emitJavaResultSignature(Formatter &out) const {
emitJavaArgResultSignature(out, results());
}
void Method::dumpAnnotations(Formatter &out) const {
if (mAnnotations->size() == 0) {
return;
}
out << "// ";
for (size_t i = 0; i < mAnnotations->size(); ++i) {
if (i > 0) {
out << " ";
}
mAnnotations->at(i)->dump(out);
}
out << "\n";
}
bool Method::deepIsJavaCompatible(std::unordered_set<const Type*>* visited) const {
if (!std::all_of(mArgs->begin(), mArgs->end(),
[&](const auto* arg) { return (*arg)->isJavaCompatible(visited); })) {
return false;
}
if (!std::all_of(mResults->begin(), mResults->end(),
[&](const auto* arg) { return (*arg)->isJavaCompatible(visited); })) {
return false;
}
return true;
}
const NamedReference<Type>* Method::canElideCallback() const {
// Can't elide callback for void or tuple-returning methods
if (mResults->size() != 1) {
return nullptr;
}
const NamedReference<Type>* typedVar = mResults->at(0);
if (typedVar->type().isElidableType()) {
return typedVar;
}
return nullptr;
}
const Location& Method::location() const {
return mLocation;
}
////////////////////////////////////////////////////////////////////////////////
bool TypedVarVector::add(NamedReference<Type>* v) {
if (mNames.emplace(v->name()).second) {
push_back(v);
return true;
}
return false;
}
} // namespace android
|