blob: e5a66b03ef1a9b352fa33371a7b41846183adcc8 [file] [log] [blame]
Andreas Huber1aec3972016-08-26 09:26:32 -07001/*
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 Huber3599d922016-08-09 10:42:57 -070017#include "Annotation.h"
18
Steven Morelanddb1b1b62017-01-10 09:50:55 -080019#include <android-base/logging.h>
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070020#include <hidl-util/Formatter.h>
Yifan Hong932464e2017-03-30 15:40:22 -070021#include <hidl-util/StringHelper.h>
Timur Iskhakovb58f4182017-08-29 15:19:24 -070022#include <algorithm>
Andreas Huber3599d922016-08-09 10:42:57 -070023#include <vector>
24
25namespace android {
26
Timur Iskhakov7a85dc22017-08-10 19:06:41 -070027AnnotationParam::AnnotationParam(const std::string& name) : mName(name) {}
Steven Morelandd537ab02016-09-12 10:32:01 -070028
Timur Iskhakov7a85dc22017-08-10 19:06:41 -070029const std::string& AnnotationParam::getName() const {
Steven Morelandd537ab02016-09-12 10:32:01 -070030 return mName;
31}
32
Timur Iskhakovb58f4182017-08-29 15:19:24 -070033std::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
41std::vector<const ConstantExpression*> AnnotationParam::getConstantExpressions() const {
Timur Iskhakov891a8662017-08-25 21:53:48 -070042 return {};
Timur Iskhakovcec46c42017-08-09 00:22:02 -070043}
44
Steven Morelanddb1b1b62017-01-10 09:50:55 -080045std::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
57bool 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 Iskhakov7a85dc22017-08-10 19:06:41 -070070StringAnnotationParam::StringAnnotationParam(const std::string& name,
71 std::vector<std::string>* values)
72 : AnnotationParam(name), mValues(values) {}
73
74std::vector<std::string> StringAnnotationParam::getValues() const {
75 return *mValues;
Andreas Huber3599d922016-08-09 10:42:57 -070076}
77
Timur Iskhakov7a85dc22017-08-10 19:06:41 -070078std::string StringAnnotationParam::getSingleValue() const {
79 CHECK_EQ(mValues->size(), 1u) << mName << " requires one value but has multiple";
80 return mValues->at(0);
81}
82
83ConstantExpressionAnnotationParam::ConstantExpressionAnnotationParam(
84 const std::string& name, std::vector<ConstantExpression*>* values)
85 : AnnotationParam(name), mValues(values) {}
86
Timur Iskhakovcec46c42017-08-09 00:22:02 -070087std::string convertToString(const ConstantExpression* value) {
Timur Iskhakov505e5612017-08-27 18:26:48 -070088 if (value->descriptionIsTrivial()) {
89 return value->value();
90 }
Timur Iskhakovcec46c42017-08-09 00:22:02 -070091 return value->value() + " /* " + value->description() + " */";
92}
93
Timur Iskhakov7a85dc22017-08-10 19:06:41 -070094std::vector<std::string> ConstantExpressionAnnotationParam::getValues() const {
95 std::vector<std::string> ret;
Timur Iskhakovcec46c42017-08-09 00:22:02 -070096 for (const auto* value : *mValues) {
97 ret.push_back(convertToString(value));
Timur Iskhakov7a85dc22017-08-10 19:06:41 -070098 };
Timur Iskhakov7a85dc22017-08-10 19:06:41 -070099 return ret;
100}
101
102std::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 Iskhakovb58f4182017-08-29 15:19:24 -0700107std::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 Iskhakov7a85dc22017-08-10 19:06:41 -0700112}
113
114Annotation::Annotation(const char* name, AnnotationParamVector* params)
115 : mName(name), mParams(params) {}
116
Andreas Huber3599d922016-08-09 10:42:57 -0700117std::string Annotation::name() const {
118 return mName;
119}
120
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700121const AnnotationParamVector &Annotation::params() const {
Steven Morelandd537ab02016-09-12 10:32:01 -0700122 return *mParams;
123}
124
Andreas Huber019d21d2016-10-03 12:59:47 -0700125const AnnotationParam *Annotation::getParam(const std::string &name) const {
Timur Iskhakov891a8662017-08-25 21:53:48 -0700126 for (const auto* i : *mParams) {
Steven Morelandd537ab02016-09-12 10:32:01 -0700127 if (i->getName() == name) {
128 return i;
129 }
130 }
131
132 return nullptr;
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700133}
134
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700135std::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
143std::vector<const ConstantExpression*> Annotation::getConstantExpressions() const {
144 std::vector<const ConstantExpression*> ret;
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700145 for (const auto* param : *mParams) {
Timur Iskhakov891a8662017-08-25 21:53:48 -0700146 const auto& retParam = param->getConstantExpressions();
147 ret.insert(ret.end(), retParam.begin(), retParam.end());
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700148 }
Timur Iskhakov891a8662017-08-25 21:53:48 -0700149 return ret;
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700150}
151
Andreas Huber3599d922016-08-09 10:42:57 -0700152void Annotation::dump(Formatter &out) const {
153 out << "@" << mName;
154
Steven Morelandd537ab02016-09-12 10:32:01 -0700155 if (mParams->size() == 0) {
Andreas Huber3599d922016-08-09 10:42:57 -0700156 return;
157 }
158
159 out << "(";
160
Steven Morelandd537ab02016-09-12 10:32:01 -0700161 for (size_t i = 0; i < mParams->size(); ++i) {
Andreas Huber3599d922016-08-09 10:42:57 -0700162 if (i > 0) {
163 out << ", ";
164 }
165
Steven Morelandd537ab02016-09-12 10:32:01 -0700166 const AnnotationParam* param = mParams->at(i);
Andreas Huber3599d922016-08-09 10:42:57 -0700167
Steven Morelandd537ab02016-09-12 10:32:01 -0700168 out << param->getName() << "=";
169
Timur Iskhakov7a85dc22017-08-10 19:06:41 -0700170 const std::vector<std::string>& values = param->getValues();
171 if (values.size() > 1) {
Andreas Huber3599d922016-08-09 10:42:57 -0700172 out << "{";
173 }
174
Timur Iskhakov7a85dc22017-08-10 19:06:41 -0700175 out << StringHelper::JoinStrings(values, ", ");
Andreas Huber3599d922016-08-09 10:42:57 -0700176
Timur Iskhakov7a85dc22017-08-10 19:06:41 -0700177 if (values.size() > 1) {
Andreas Huber3599d922016-08-09 10:42:57 -0700178 out << "}";
179 }
180 }
181
182 out << ")";
183}
184
185} // namespace android
186