blob: dc02b69f7b704613514b78695a29bc4bdef25c60 [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
Timur Iskhakov63f39902017-08-29 15:47:29 -070023ScalarType::ScalarType(Kind kind, Scope* parent) : Type(parent), mKind(kind) {}
Andreas Huberc9410c72016-07-28 12:18:40 -070024
Andreas Huber737080b2016-08-02 15:38:04 -070025const ScalarType *ScalarType::resolveToScalarType() const {
26 return this;
27}
28
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070029bool ScalarType::isValidEnumStorageType() const {
30 // Only integer types.
31 return mKind >= KIND_INT8 && mKind <= KIND_UINT64;
32}
33
Yifan Hongabf73ee2016-12-05 18:47:00 -080034bool ScalarType::isScalar() const {
35 return true;
36}
37
Steven Moreland9df52442016-12-12 08:51:14 -080038bool ScalarType::isElidableType() const {
39 return true;
40}
41
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -070042bool ScalarType::deepCanCheckEquality(std::unordered_set<const Type*>* /* visited */) const {
Yifan Hongc6752dc2016-12-20 14:00:14 -080043 return true;
44}
45
Steven Moreland30bb6a82016-11-30 09:18:34 -080046std::string ScalarType::typeName() const {
47 return getCppStackType();
48}
49
Yifan Hong3b320f82016-11-01 15:15:54 -070050std::string ScalarType::getCppType(StorageMode, bool) const {
Andreas Huber881227d2016-08-02 14:20:21 -070051 static const char *const kName[] = {
Andreas Huber881227d2016-08-02 14:20:21 -070052 "bool",
Andreas Huber881227d2016-08-02 14:20:21 -070053 "int8_t",
54 "uint8_t",
55 "int16_t",
56 "uint16_t",
57 "int32_t",
58 "uint32_t",
59 "int64_t",
60 "uint64_t",
61 "float",
62 "double"
63 };
64
Andreas Huber881227d2016-08-02 14:20:21 -070065 return kName[mKind];
66}
67
Yifan Hong4ed13472016-11-02 10:44:11 -070068std::string ScalarType::getJavaType(bool /* forInitializer */) const {
Andreas Huber2831d512016-08-15 09:33:47 -070069 static const char *const kName[] = {
Andreas Huber2831d512016-08-15 09:33:47 -070070 "boolean",
Andreas Huber2831d512016-08-15 09:33:47 -070071 "byte",
72 "byte",
73 "short",
74 "short",
75 "int",
76 "int",
77 "long",
78 "long",
79 "float",
80 "double"
81 };
82
83 return kName[mKind];
84}
85
Nirav Atre66842a92018-06-28 18:14:13 -070086std::string ScalarType::getJavaTypeClass() const {
Andreas Huber85eabdb2016-08-25 11:24:49 -070087 static const char *const kName[] = {
88 "Boolean",
Andreas Huber85eabdb2016-08-25 11:24:49 -070089 "Byte",
90 "Byte",
91 "Short",
92 "Short",
Andreas Huber5001d932016-09-14 09:09:09 -070093 "Integer",
94 "Integer",
Andreas Huber85eabdb2016-08-25 11:24:49 -070095 "Long",
96 "Long",
97 "Float",
98 "Double"
99 };
100
101 return kName[mKind];
102}
103
Andreas Huber2831d512016-08-15 09:33:47 -0700104std::string ScalarType::getJavaSuffix() const {
105 static const char *const kSuffix[] = {
Andreas Huberd85a5092016-08-25 09:51:52 -0700106 "Bool",
Andreas Huber2831d512016-08-15 09:33:47 -0700107 "Int8",
108 "Int8",
109 "Int16",
110 "Int16",
111 "Int32",
112 "Int32",
113 "Int64",
114 "Int64",
115 "Float",
116 "Double"
117 };
118
119 return kSuffix[mKind];
120}
121
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700122std::string ScalarType::getVtsType() const {
123 return "TYPE_SCALAR";
124}
125
126std::string ScalarType::getVtsScalarType() const {
127 static const char * const kName[] = {
128 "bool_t",
129 "int8_t",
130 "uint8_t",
131 "int16_t",
132 "uint16_t",
133 "int32_t",
134 "uint32_t",
135 "int64_t",
136 "uint64_t",
137 "float_t",
138 "double_t"
139 };
140
141 return kName[mKind];
142}
143
Andreas Huber881227d2016-08-02 14:20:21 -0700144void ScalarType::emitReaderWriter(
145 Formatter &out,
146 const std::string &name,
147 const std::string &parcelObj,
148 bool parcelObjIsPointer,
149 bool isReader,
150 ErrorMode mode) const {
Andreas Huber737080b2016-08-02 15:38:04 -0700151 emitReaderWriterWithCast(
152 out,
153 name,
154 parcelObj,
155 parcelObjIsPointer,
156 isReader,
157 mode,
158 false /* needsCast */);
159}
160
161void ScalarType::emitReaderWriterWithCast(
162 Formatter &out,
163 const std::string &name,
164 const std::string &parcelObj,
165 bool parcelObjIsPointer,
166 bool isReader,
167 ErrorMode mode,
168 bool needsCast) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700169 static const char *const kSuffix[] = {
Iliyan Malcheve0b28672016-08-14 13:35:12 -0700170 "Bool",
Andreas Huber881227d2016-08-02 14:20:21 -0700171 "Int8",
172 "Uint8",
173 "Int16",
174 "Uint16",
175 "Int32",
176 "Uint32",
177 "Int64",
178 "Uint64",
179 "Float",
180 "Double"
181 };
182
183 const std::string parcelObjDeref =
184 parcelObj + (parcelObjIsPointer ? "->" : ".");
185
Iliyan Malchev549e2592016-08-10 08:59:12 -0700186 out << "_hidl_err = "
Andreas Huber881227d2016-08-02 14:20:21 -0700187 << parcelObjDeref
188 << (isReader ? "read" : "write")
189 << kSuffix[mKind]
190 << "(";
191
Andreas Huber737080b2016-08-02 15:38:04 -0700192 if (needsCast) {
Andreas Huber737080b2016-08-02 15:38:04 -0700193 out << "("
Yifan Hong3b320f82016-11-01 15:15:54 -0700194 << getCppStackType()
Andreas Huber737080b2016-08-02 15:38:04 -0700195 << (isReader ? " *)" : ")");
196 }
197
Andreas Huber881227d2016-08-02 14:20:21 -0700198 if (isReader) {
199 out << "&";
200 }
201
202 out << name
203 << ");\n";
204
205 handleError(out, mode);
206}
207
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800208void ScalarType::emitHexDump(
209 Formatter &out,
210 const std::string &streamName,
211 const std::string &name) const {
212 out << streamName << " += toHexString(" << name << ");\n";
213}
214
Yifan Honge45b5302017-02-22 10:49:07 -0800215void ScalarType::emitConvertToJavaHexString(
216 Formatter &out,
217 const std::string &name) const {
218 switch(mKind) {
219 case KIND_BOOL: {
220 out << "((" << name << ") ? \"0x1\" : \"0x0\")";
221 break;
222 }
223 case KIND_INT8: // fallthrough
224 case KIND_UINT8: // fallthrough
225 case KIND_INT16: // fallthrough
226 case KIND_UINT16: {
227 // Because Byte and Short doesn't have toHexString, we have to use Integer.toHexString.
Nirav Atre66842a92018-06-28 18:14:13 -0700228 out << "Integer.toHexString(" << getJavaTypeClass() << ".toUnsignedInt(("
Yifan Honge45b5302017-02-22 10:49:07 -0800229 << getJavaType(false /* forInitializer */) << ")(" << name << ")))";
230 break;
231 }
232 case KIND_INT32: // fallthrough
233 case KIND_UINT32: // fallthrough
234 case KIND_INT64: // fallthrough
235 case KIND_UINT64: {
Nirav Atre66842a92018-06-28 18:14:13 -0700236 out << getJavaTypeClass() << ".toHexString(" << name << ")";
Yifan Honge45b5302017-02-22 10:49:07 -0800237 break;
238 }
239 case KIND_FLOAT: // fallthrough
240 case KIND_DOUBLE: // fallthrough
241 default: {
242 // no hex for floating point numbers.
243 out << name;
244 break;
245 }
246 }
247}
248
Andreas Huber85eabdb2016-08-25 11:24:49 -0700249void ScalarType::emitJavaFieldReaderWriter(
250 Formatter &out,
Andreas Huber4c865b72016-09-14 15:26:27 -0700251 size_t /* depth */,
Andreas Huber709b62d2016-09-19 11:21:18 -0700252 const std::string & /* parcelName */,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700253 const std::string &blobName,
254 const std::string &fieldName,
255 const std::string &offset,
256 bool isReader) const {
257 if (isReader) {
258 out << fieldName
259 << " = "
260 << blobName
261 << ".get"
262 << getJavaSuffix()
263 << "("
264 << offset
265 << ");\n";
266
267 return;
268 }
269
270 out << blobName
271 << ".put"
272 << getJavaSuffix()
273 << "("
274 << offset
275 << ", "
276 << fieldName
277 << ");\n";
278}
279
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800280void ScalarType::emitVtsTypeDeclarations(Formatter& out) const {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700281 out << "type: " << getVtsType() << "\n";
282 out << "scalar_type: \"" << getVtsScalarType() << "\"\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700283}
284
Andreas Huber85eabdb2016-08-25 11:24:49 -0700285void ScalarType::getAlignmentAndSize(size_t *align, size_t *size) const {
286 static const size_t kAlign[] = {
287 1, // bool, this is NOT standardized!
Andreas Huber85eabdb2016-08-25 11:24:49 -0700288 1, // int8_t
289 1, // uint8_t
290 2, // int16_t
291 2, // uint16_t
292 4, // int32_t
293 4, // uint32_t
294 8, // int64_t
295 8, // uint64_t
296 4, // float
297 8 // double
298 };
299
300 *align = *size = kAlign[mKind];
301}
302
Yifan Hong57886972016-08-17 10:42:15 -0700303ScalarType::Kind ScalarType::getKind() const {
304 return mKind;
305}
306
Andreas Huberc9410c72016-07-28 12:18:40 -0700307} // namespace android
308