blob: fefd418e147fa46a506ecab6576d7ecc0d53f49e [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
Yifan Hongabf73ee2016-12-05 18:47:00 -080036bool ScalarType::isScalar() const {
37 return true;
38}
39
Steven Moreland9df52442016-12-12 08:51:14 -080040bool ScalarType::isElidableType() const {
41 return true;
42}
43
Yifan Hongc6752dc2016-12-20 14:00:14 -080044bool ScalarType::canCheckEquality() const {
45 return true;
46}
47
Steven Moreland30bb6a82016-11-30 09:18:34 -080048std::string ScalarType::typeName() const {
49 return getCppStackType();
50}
51
Yifan Hong3b320f82016-11-01 15:15:54 -070052std::string ScalarType::getCppType(StorageMode, bool) const {
Andreas Huber881227d2016-08-02 14:20:21 -070053 static const char *const kName[] = {
Andreas Huber881227d2016-08-02 14:20:21 -070054 "bool",
Andreas Huber881227d2016-08-02 14:20:21 -070055 "int8_t",
56 "uint8_t",
57 "int16_t",
58 "uint16_t",
59 "int32_t",
60 "uint32_t",
61 "int64_t",
62 "uint64_t",
63 "float",
64 "double"
65 };
66
Andreas Huber881227d2016-08-02 14:20:21 -070067 return kName[mKind];
68}
69
Yifan Hong4ed13472016-11-02 10:44:11 -070070std::string ScalarType::getJavaType(bool /* forInitializer */) const {
Andreas Huber2831d512016-08-15 09:33:47 -070071 static const char *const kName[] = {
Andreas Huber2831d512016-08-15 09:33:47 -070072 "boolean",
Andreas Huber2831d512016-08-15 09:33:47 -070073 "byte",
74 "byte",
75 "short",
76 "short",
77 "int",
78 "int",
79 "long",
80 "long",
81 "float",
82 "double"
83 };
84
85 return kName[mKind];
86}
87
Andreas Huber85eabdb2016-08-25 11:24:49 -070088std::string ScalarType::getJavaWrapperType() const {
89 static const char *const kName[] = {
90 "Boolean",
Andreas Huber85eabdb2016-08-25 11:24:49 -070091 "Byte",
92 "Byte",
93 "Short",
94 "Short",
Andreas Huber5001d932016-09-14 09:09:09 -070095 "Integer",
96 "Integer",
Andreas Huber85eabdb2016-08-25 11:24:49 -070097 "Long",
98 "Long",
99 "Float",
100 "Double"
101 };
102
103 return kName[mKind];
104}
105
Andreas Huber2831d512016-08-15 09:33:47 -0700106std::string ScalarType::getJavaSuffix() const {
107 static const char *const kSuffix[] = {
Andreas Huberd85a5092016-08-25 09:51:52 -0700108 "Bool",
Andreas Huber2831d512016-08-15 09:33:47 -0700109 "Int8",
110 "Int8",
111 "Int16",
112 "Int16",
113 "Int32",
114 "Int32",
115 "Int64",
116 "Int64",
117 "Float",
118 "Double"
119 };
120
121 return kSuffix[mKind];
122}
123
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700124std::string ScalarType::getVtsType() const {
125 return "TYPE_SCALAR";
126}
127
128std::string ScalarType::getVtsScalarType() const {
129 static const char * const kName[] = {
130 "bool_t",
131 "int8_t",
132 "uint8_t",
133 "int16_t",
134 "uint16_t",
135 "int32_t",
136 "uint32_t",
137 "int64_t",
138 "uint64_t",
139 "float_t",
140 "double_t"
141 };
142
143 return kName[mKind];
144}
145
Andreas Huber881227d2016-08-02 14:20:21 -0700146void ScalarType::emitReaderWriter(
147 Formatter &out,
148 const std::string &name,
149 const std::string &parcelObj,
150 bool parcelObjIsPointer,
151 bool isReader,
152 ErrorMode mode) const {
Andreas Huber737080b2016-08-02 15:38:04 -0700153 emitReaderWriterWithCast(
154 out,
155 name,
156 parcelObj,
157 parcelObjIsPointer,
158 isReader,
159 mode,
160 false /* needsCast */);
161}
162
163void ScalarType::emitReaderWriterWithCast(
164 Formatter &out,
165 const std::string &name,
166 const std::string &parcelObj,
167 bool parcelObjIsPointer,
168 bool isReader,
169 ErrorMode mode,
170 bool needsCast) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700171 static const char *const kSuffix[] = {
Iliyan Malcheve0b28672016-08-14 13:35:12 -0700172 "Bool",
Andreas Huber881227d2016-08-02 14:20:21 -0700173 "Int8",
174 "Uint8",
175 "Int16",
176 "Uint16",
177 "Int32",
178 "Uint32",
179 "Int64",
180 "Uint64",
181 "Float",
182 "Double"
183 };
184
185 const std::string parcelObjDeref =
186 parcelObj + (parcelObjIsPointer ? "->" : ".");
187
Iliyan Malchev549e2592016-08-10 08:59:12 -0700188 out << "_hidl_err = "
Andreas Huber881227d2016-08-02 14:20:21 -0700189 << parcelObjDeref
190 << (isReader ? "read" : "write")
191 << kSuffix[mKind]
192 << "(";
193
Andreas Huber737080b2016-08-02 15:38:04 -0700194 if (needsCast) {
Andreas Huber737080b2016-08-02 15:38:04 -0700195 out << "("
Yifan Hong3b320f82016-11-01 15:15:54 -0700196 << getCppStackType()
Andreas Huber737080b2016-08-02 15:38:04 -0700197 << (isReader ? " *)" : ")");
198 }
199
Andreas Huber881227d2016-08-02 14:20:21 -0700200 if (isReader) {
201 out << "&";
202 }
203
204 out << name
205 << ");\n";
206
207 handleError(out, mode);
208}
209
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800210void ScalarType::emitHexDump(
211 Formatter &out,
212 const std::string &streamName,
213 const std::string &name) const {
214 out << streamName << " += toHexString(" << name << ");\n";
215}
216
Yifan Honge45b5302017-02-22 10:49:07 -0800217void ScalarType::emitConvertToJavaHexString(
218 Formatter &out,
219 const std::string &name) const {
220 switch(mKind) {
221 case KIND_BOOL: {
222 out << "((" << name << ") ? \"0x1\" : \"0x0\")";
223 break;
224 }
225 case KIND_INT8: // fallthrough
226 case KIND_UINT8: // fallthrough
227 case KIND_INT16: // fallthrough
228 case KIND_UINT16: {
229 // Because Byte and Short doesn't have toHexString, we have to use Integer.toHexString.
230 out << "Integer.toHexString(" << getJavaWrapperType() << ".toUnsignedInt(("
231 << getJavaType(false /* forInitializer */) << ")(" << name << ")))";
232 break;
233 }
234 case KIND_INT32: // fallthrough
235 case KIND_UINT32: // fallthrough
236 case KIND_INT64: // fallthrough
237 case KIND_UINT64: {
238 out << getJavaWrapperType() << ".toHexString(" << name << ")";
239 break;
240 }
241 case KIND_FLOAT: // fallthrough
242 case KIND_DOUBLE: // fallthrough
243 default: {
244 // no hex for floating point numbers.
245 out << name;
246 break;
247 }
248 }
249}
250
Andreas Huber85eabdb2016-08-25 11:24:49 -0700251void ScalarType::emitJavaFieldReaderWriter(
252 Formatter &out,
Andreas Huber4c865b72016-09-14 15:26:27 -0700253 size_t /* depth */,
Andreas Huber709b62d2016-09-19 11:21:18 -0700254 const std::string & /* parcelName */,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700255 const std::string &blobName,
256 const std::string &fieldName,
257 const std::string &offset,
258 bool isReader) const {
259 if (isReader) {
260 out << fieldName
261 << " = "
262 << blobName
263 << ".get"
264 << getJavaSuffix()
265 << "("
266 << offset
267 << ");\n";
268
269 return;
270 }
271
272 out << blobName
273 << ".put"
274 << getJavaSuffix()
275 << "("
276 << offset
277 << ", "
278 << fieldName
279 << ");\n";
280}
281
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700282status_t ScalarType::emitVtsTypeDeclarations(Formatter &out) const {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700283 out << "type: " << getVtsType() << "\n";
284 out << "scalar_type: \"" << getVtsScalarType() << "\"\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700285 return OK;
286}
287
Andreas Huber85eabdb2016-08-25 11:24:49 -0700288void ScalarType::getAlignmentAndSize(size_t *align, size_t *size) const {
289 static const size_t kAlign[] = {
290 1, // bool, this is NOT standardized!
Andreas Huber85eabdb2016-08-25 11:24:49 -0700291 1, // int8_t
292 1, // uint8_t
293 2, // int16_t
294 2, // uint16_t
295 4, // int32_t
296 4, // uint32_t
297 8, // int64_t
298 8, // uint64_t
299 4, // float
300 8 // double
301 };
302
303 *align = *size = kAlign[mKind];
304}
305
Yifan Hong57886972016-08-17 10:42:15 -0700306ScalarType::Kind ScalarType::getKind() const {
307 return mKind;
308}
309
Andreas Huberc9410c72016-07-28 12:18:40 -0700310} // namespace android
311