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 | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 17 | #include "Annotation.h" |
| 18 | |
Steven Moreland | db1b1b6 | 2017-01-10 09:50:55 -0800 | [diff] [blame] | 19 | #include <android-base/logging.h> |
Iliyan Malchev | a72e0d2 | 2016-09-09 11:03:08 -0700 | [diff] [blame] | 20 | #include <hidl-util/Formatter.h> |
Yifan Hong | 932464e | 2017-03-30 15:40:22 -0700 | [diff] [blame] | 21 | #include <hidl-util/StringHelper.h> |
Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 22 | #include <algorithm> |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 23 | #include <vector> |
| 24 | |
| 25 | namespace android { |
| 26 | |
Timur Iskhakov | 7a85dc2 | 2017-08-10 19:06:41 -0700 | [diff] [blame] | 27 | AnnotationParam::AnnotationParam(const std::string& name) : mName(name) {} |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 28 | |
Timur Iskhakov | 7a85dc2 | 2017-08-10 19:06:41 -0700 | [diff] [blame] | 29 | const std::string& AnnotationParam::getName() const { |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 30 | return mName; |
| 31 | } |
| 32 | |
Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 33 | std::vector<ConstantExpression*> AnnotationParam::getConstantExpressions() { |
| 34 | const auto& constRet = static_cast<const AnnotationParam*>(this)->getConstantExpressions(); |
| 35 | std::vector<ConstantExpression*> ret(constRet.size()); |
| 36 | std::transform(constRet.begin(), constRet.end(), ret.begin(), |
| 37 | [](const auto* ce) { return const_cast<ConstantExpression*>(ce); }); |
| 38 | return ret; |
| 39 | } |
| 40 | |
| 41 | std::vector<const ConstantExpression*> AnnotationParam::getConstantExpressions() const { |
Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 42 | return {}; |
Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Steven Moreland | db1b1b6 | 2017-01-10 09:50:55 -0800 | [diff] [blame] | 45 | std::string AnnotationParam::getSingleString() const { |
| 46 | std::string value = getSingleValue(); |
| 47 | |
| 48 | CHECK(value.size() >= 2 && value[0] == '"' && value[value.size() - 1] == '"') |
| 49 | << mName << " must be a string"; |
| 50 | |
| 51 | // unquote string |
| 52 | value = value.substr(1, value.size() - 2); |
| 53 | |
| 54 | return value; |
| 55 | } |
| 56 | |
| 57 | bool AnnotationParam::getSingleBool() const { |
| 58 | std::string value = getSingleString(); |
| 59 | |
| 60 | if (value == "true") { |
| 61 | return true; |
| 62 | } else if (value == "false") { |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | CHECK(false) << mName << " must be of boolean value (true/false)."; |
| 67 | return false; |
| 68 | } |
| 69 | |
Timur Iskhakov | 7a85dc2 | 2017-08-10 19:06:41 -0700 | [diff] [blame] | 70 | StringAnnotationParam::StringAnnotationParam(const std::string& name, |
| 71 | std::vector<std::string>* values) |
| 72 | : AnnotationParam(name), mValues(values) {} |
| 73 | |
| 74 | std::vector<std::string> StringAnnotationParam::getValues() const { |
| 75 | return *mValues; |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Timur Iskhakov | 7a85dc2 | 2017-08-10 19:06:41 -0700 | [diff] [blame] | 78 | std::string StringAnnotationParam::getSingleValue() const { |
| 79 | CHECK_EQ(mValues->size(), 1u) << mName << " requires one value but has multiple"; |
| 80 | return mValues->at(0); |
| 81 | } |
| 82 | |
| 83 | ConstantExpressionAnnotationParam::ConstantExpressionAnnotationParam( |
| 84 | const std::string& name, std::vector<ConstantExpression*>* values) |
| 85 | : AnnotationParam(name), mValues(values) {} |
| 86 | |
Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 87 | std::string convertToString(const ConstantExpression* value) { |
Timur Iskhakov | 505e561 | 2017-08-27 18:26:48 -0700 | [diff] [blame] | 88 | if (value->descriptionIsTrivial()) { |
| 89 | return value->value(); |
| 90 | } |
Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 91 | return value->value() + " /* " + value->description() + " */"; |
| 92 | } |
| 93 | |
Timur Iskhakov | 7a85dc2 | 2017-08-10 19:06:41 -0700 | [diff] [blame] | 94 | std::vector<std::string> ConstantExpressionAnnotationParam::getValues() const { |
| 95 | std::vector<std::string> ret; |
Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 96 | for (const auto* value : *mValues) { |
| 97 | ret.push_back(convertToString(value)); |
Timur Iskhakov | 7a85dc2 | 2017-08-10 19:06:41 -0700 | [diff] [blame] | 98 | }; |
Timur Iskhakov | 7a85dc2 | 2017-08-10 19:06:41 -0700 | [diff] [blame] | 99 | return ret; |
| 100 | } |
| 101 | |
| 102 | std::string ConstantExpressionAnnotationParam::getSingleValue() const { |
| 103 | CHECK_EQ(mValues->size(), 1u) << mName << " requires one value but has multiple"; |
| 104 | return convertToString(mValues->at(0)); |
| 105 | } |
| 106 | |
Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 107 | std::vector<const ConstantExpression*> ConstantExpressionAnnotationParam::getConstantExpressions() |
| 108 | const { |
| 109 | std::vector<const ConstantExpression*> ret; |
| 110 | ret.insert(ret.end(), mValues->begin(), mValues->end()); |
| 111 | return ret; |
Timur Iskhakov | 7a85dc2 | 2017-08-10 19:06:41 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | Annotation::Annotation(const char* name, AnnotationParamVector* params) |
| 115 | : mName(name), mParams(params) {} |
| 116 | |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 117 | std::string Annotation::name() const { |
| 118 | return mName; |
| 119 | } |
| 120 | |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 121 | const AnnotationParamVector &Annotation::params() const { |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 122 | return *mParams; |
| 123 | } |
| 124 | |
Andreas Huber | 019d21d | 2016-10-03 12:59:47 -0700 | [diff] [blame] | 125 | const AnnotationParam *Annotation::getParam(const std::string &name) const { |
Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 126 | for (const auto* i : *mParams) { |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 127 | if (i->getName() == name) { |
| 128 | return i; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | return nullptr; |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 135 | std::vector<ConstantExpression*> Annotation::getConstantExpressions() { |
| 136 | const auto& constRet = static_cast<const Annotation*>(this)->getConstantExpressions(); |
| 137 | std::vector<ConstantExpression*> ret(constRet.size()); |
| 138 | std::transform(constRet.begin(), constRet.end(), ret.begin(), |
| 139 | [](const auto* ce) { return const_cast<ConstantExpression*>(ce); }); |
| 140 | return ret; |
| 141 | } |
| 142 | |
| 143 | std::vector<const ConstantExpression*> Annotation::getConstantExpressions() const { |
| 144 | std::vector<const ConstantExpression*> ret; |
Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 145 | for (const auto* param : *mParams) { |
Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 146 | const auto& retParam = param->getConstantExpressions(); |
| 147 | ret.insert(ret.end(), retParam.begin(), retParam.end()); |
Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 148 | } |
Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 149 | return ret; |
Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 152 | void Annotation::dump(Formatter &out) const { |
| 153 | out << "@" << mName; |
| 154 | |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 155 | if (mParams->size() == 0) { |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 156 | return; |
| 157 | } |
| 158 | |
| 159 | out << "("; |
| 160 | |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 161 | for (size_t i = 0; i < mParams->size(); ++i) { |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 162 | if (i > 0) { |
| 163 | out << ", "; |
| 164 | } |
| 165 | |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 166 | const AnnotationParam* param = mParams->at(i); |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 167 | |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 168 | out << param->getName() << "="; |
| 169 | |
Timur Iskhakov | 7a85dc2 | 2017-08-10 19:06:41 -0700 | [diff] [blame] | 170 | const std::vector<std::string>& values = param->getValues(); |
| 171 | if (values.size() > 1) { |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 172 | out << "{"; |
| 173 | } |
| 174 | |
Timur Iskhakov | 7a85dc2 | 2017-08-10 19:06:41 -0700 | [diff] [blame] | 175 | out << StringHelper::JoinStrings(values, ", "); |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 176 | |
Timur Iskhakov | 7a85dc2 | 2017-08-10 19:06:41 -0700 | [diff] [blame] | 177 | if (values.size() > 1) { |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 178 | out << "}"; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | out << ")"; |
| 183 | } |
| 184 | |
| 185 | } // namespace android |
| 186 | |