blob: 0e4beaf56a794c4a24ccd299b0b8edad7bcd1653 [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 Huberc9410c72016-07-28 12:18:40 -070017#include "ScalarType.h"
18
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070019#include <hidl-util/Formatter.h>
Andreas Huberc9410c72016-07-28 12:18:40 -070020
21namespace android {
22
23ScalarType::ScalarType(Kind kind)
24 : mKind(kind) {
25}
26
Andreas Huber737080b2016-08-02 15:38:04 -070027const ScalarType *ScalarType::resolveToScalarType() const {
28 return this;
29}
30
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070031bool ScalarType::isValidEnumStorageType() const {
32 // Only integer types.
33 return mKind >= KIND_INT8 && mKind <= KIND_UINT64;
34}
35
Steven Moreland979e0992016-09-07 09:18:08 -070036void ScalarType::addNamedTypesToSet(std::set<const FQName> &) const {
37 // do nothing
38}
39
Yifan Hong3b320f82016-11-01 15:15:54 -070040std::string ScalarType::getCppType(StorageMode, bool) const {
Andreas Huber881227d2016-08-02 14:20:21 -070041 static const char *const kName[] = {
Andreas Huber881227d2016-08-02 14:20:21 -070042 "bool",
Andreas Huber881227d2016-08-02 14:20:21 -070043 "int8_t",
44 "uint8_t",
45 "int16_t",
46 "uint16_t",
47 "int32_t",
48 "uint32_t",
49 "int64_t",
50 "uint64_t",
51 "float",
52 "double"
53 };
54
Andreas Huber881227d2016-08-02 14:20:21 -070055 return kName[mKind];
56}
57
Yifan Hong4ed13472016-11-02 10:44:11 -070058std::string ScalarType::getJavaType(bool /* forInitializer */) const {
Andreas Huber2831d512016-08-15 09:33:47 -070059 static const char *const kName[] = {
Andreas Huber2831d512016-08-15 09:33:47 -070060 "boolean",
Andreas Huber2831d512016-08-15 09:33:47 -070061 "byte",
62 "byte",
63 "short",
64 "short",
65 "int",
66 "int",
67 "long",
68 "long",
69 "float",
70 "double"
71 };
72
73 return kName[mKind];
74}
75
Andreas Huber85eabdb2016-08-25 11:24:49 -070076std::string ScalarType::getJavaWrapperType() const {
77 static const char *const kName[] = {
78 "Boolean",
Andreas Huber85eabdb2016-08-25 11:24:49 -070079 "Byte",
80 "Byte",
81 "Short",
82 "Short",
Andreas Huber5001d932016-09-14 09:09:09 -070083 "Integer",
84 "Integer",
Andreas Huber85eabdb2016-08-25 11:24:49 -070085 "Long",
86 "Long",
87 "Float",
88 "Double"
89 };
90
91 return kName[mKind];
92}
93
Andreas Huber2831d512016-08-15 09:33:47 -070094std::string ScalarType::getJavaSuffix() const {
95 static const char *const kSuffix[] = {
Andreas Huberd85a5092016-08-25 09:51:52 -070096 "Bool",
Andreas Huber2831d512016-08-15 09:33:47 -070097 "Int8",
98 "Int8",
99 "Int16",
100 "Int16",
101 "Int32",
102 "Int32",
103 "Int64",
104 "Int64",
105 "Float",
106 "Double"
107 };
108
109 return kSuffix[mKind];
110}
111
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700112std::string ScalarType::getVtsType() const {
113 return "TYPE_SCALAR";
114}
115
116std::string ScalarType::getVtsScalarType() const {
117 static const char * const kName[] = {
118 "bool_t",
119 "int8_t",
120 "uint8_t",
121 "int16_t",
122 "uint16_t",
123 "int32_t",
124 "uint32_t",
125 "int64_t",
126 "uint64_t",
127 "float_t",
128 "double_t"
129 };
130
131 return kName[mKind];
132}
133
Andreas Huber881227d2016-08-02 14:20:21 -0700134void ScalarType::emitReaderWriter(
135 Formatter &out,
136 const std::string &name,
137 const std::string &parcelObj,
138 bool parcelObjIsPointer,
139 bool isReader,
140 ErrorMode mode) const {
Andreas Huber737080b2016-08-02 15:38:04 -0700141 emitReaderWriterWithCast(
142 out,
143 name,
144 parcelObj,
145 parcelObjIsPointer,
146 isReader,
147 mode,
148 false /* needsCast */);
149}
150
151void ScalarType::emitReaderWriterWithCast(
152 Formatter &out,
153 const std::string &name,
154 const std::string &parcelObj,
155 bool parcelObjIsPointer,
156 bool isReader,
157 ErrorMode mode,
158 bool needsCast) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700159 static const char *const kSuffix[] = {
Iliyan Malcheve0b28672016-08-14 13:35:12 -0700160 "Bool",
Andreas Huber881227d2016-08-02 14:20:21 -0700161 "Int8",
162 "Uint8",
163 "Int16",
164 "Uint16",
165 "Int32",
166 "Uint32",
167 "Int64",
168 "Uint64",
169 "Float",
170 "Double"
171 };
172
173 const std::string parcelObjDeref =
174 parcelObj + (parcelObjIsPointer ? "->" : ".");
175
Iliyan Malchev549e2592016-08-10 08:59:12 -0700176 out << "_hidl_err = "
Andreas Huber881227d2016-08-02 14:20:21 -0700177 << parcelObjDeref
178 << (isReader ? "read" : "write")
179 << kSuffix[mKind]
180 << "(";
181
Andreas Huber737080b2016-08-02 15:38:04 -0700182 if (needsCast) {
Andreas Huber737080b2016-08-02 15:38:04 -0700183 out << "("
Yifan Hong3b320f82016-11-01 15:15:54 -0700184 << getCppStackType()
Andreas Huber737080b2016-08-02 15:38:04 -0700185 << (isReader ? " *)" : ")");
186 }
187
Andreas Huber881227d2016-08-02 14:20:21 -0700188 if (isReader) {
189 out << "&";
190 }
191
192 out << name
193 << ");\n";
194
195 handleError(out, mode);
196}
197
Andreas Huber85eabdb2016-08-25 11:24:49 -0700198void ScalarType::emitJavaFieldReaderWriter(
199 Formatter &out,
Andreas Huber4c865b72016-09-14 15:26:27 -0700200 size_t /* depth */,
Andreas Huber709b62d2016-09-19 11:21:18 -0700201 const std::string & /* parcelName */,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700202 const std::string &blobName,
203 const std::string &fieldName,
204 const std::string &offset,
205 bool isReader) const {
206 if (isReader) {
207 out << fieldName
208 << " = "
209 << blobName
210 << ".get"
211 << getJavaSuffix()
212 << "("
213 << offset
214 << ");\n";
215
216 return;
217 }
218
219 out << blobName
220 << ".put"
221 << getJavaSuffix()
222 << "("
223 << offset
224 << ", "
225 << fieldName
226 << ");\n";
227}
228
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700229status_t ScalarType::emitVtsTypeDeclarations(Formatter &out) const {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700230 out << "type: " << getVtsType() << "\n";
231 out << "scalar_type: \"" << getVtsScalarType() << "\"\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700232 return OK;
233}
234
Andreas Huber85eabdb2016-08-25 11:24:49 -0700235void ScalarType::getAlignmentAndSize(size_t *align, size_t *size) const {
236 static const size_t kAlign[] = {
237 1, // bool, this is NOT standardized!
Andreas Huber85eabdb2016-08-25 11:24:49 -0700238 1, // int8_t
239 1, // uint8_t
240 2, // int16_t
241 2, // uint16_t
242 4, // int32_t
243 4, // uint32_t
244 8, // int64_t
245 8, // uint64_t
246 4, // float
247 8 // double
248 };
249
250 *align = *size = kAlign[mKind];
251}
252
Yifan Hong57886972016-08-17 10:42:15 -0700253ScalarType::Kind ScalarType::getKind() const {
254 return mKind;
255}
256
Andreas Huberc9410c72016-07-28 12:18:40 -0700257} // namespace android
258