blob: 9b6c0c3d5bd572f224f7d2ab85780dddfd80f7b7 [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 Hongabf73ee2016-12-05 18:47:00 -080040bool ScalarType::isScalar() const {
41 return true;
42}
43
Steven Moreland9df52442016-12-12 08:51:14 -080044bool ScalarType::isElidableType() const {
45 return true;
46}
47
Yifan Hongc6752dc2016-12-20 14:00:14 -080048bool ScalarType::canCheckEquality() const {
49 return true;
50}
51
Steven Moreland30bb6a82016-11-30 09:18:34 -080052std::string ScalarType::typeName() const {
53 return getCppStackType();
54}
55
Yifan Hong3b320f82016-11-01 15:15:54 -070056std::string ScalarType::getCppType(StorageMode, bool) const {
Andreas Huber881227d2016-08-02 14:20:21 -070057 static const char *const kName[] = {
Andreas Huber881227d2016-08-02 14:20:21 -070058 "bool",
Andreas Huber881227d2016-08-02 14:20:21 -070059 "int8_t",
60 "uint8_t",
61 "int16_t",
62 "uint16_t",
63 "int32_t",
64 "uint32_t",
65 "int64_t",
66 "uint64_t",
67 "float",
68 "double"
69 };
70
Andreas Huber881227d2016-08-02 14:20:21 -070071 return kName[mKind];
72}
73
Yifan Hong4ed13472016-11-02 10:44:11 -070074std::string ScalarType::getJavaType(bool /* forInitializer */) const {
Andreas Huber2831d512016-08-15 09:33:47 -070075 static const char *const kName[] = {
Andreas Huber2831d512016-08-15 09:33:47 -070076 "boolean",
Andreas Huber2831d512016-08-15 09:33:47 -070077 "byte",
78 "byte",
79 "short",
80 "short",
81 "int",
82 "int",
83 "long",
84 "long",
85 "float",
86 "double"
87 };
88
89 return kName[mKind];
90}
91
Andreas Huber85eabdb2016-08-25 11:24:49 -070092std::string ScalarType::getJavaWrapperType() const {
93 static const char *const kName[] = {
94 "Boolean",
Andreas Huber85eabdb2016-08-25 11:24:49 -070095 "Byte",
96 "Byte",
97 "Short",
98 "Short",
Andreas Huber5001d932016-09-14 09:09:09 -070099 "Integer",
100 "Integer",
Andreas Huber85eabdb2016-08-25 11:24:49 -0700101 "Long",
102 "Long",
103 "Float",
104 "Double"
105 };
106
107 return kName[mKind];
108}
109
Andreas Huber2831d512016-08-15 09:33:47 -0700110std::string ScalarType::getJavaSuffix() const {
111 static const char *const kSuffix[] = {
Andreas Huberd85a5092016-08-25 09:51:52 -0700112 "Bool",
Andreas Huber2831d512016-08-15 09:33:47 -0700113 "Int8",
114 "Int8",
115 "Int16",
116 "Int16",
117 "Int32",
118 "Int32",
119 "Int64",
120 "Int64",
121 "Float",
122 "Double"
123 };
124
125 return kSuffix[mKind];
126}
127
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700128std::string ScalarType::getVtsType() const {
129 return "TYPE_SCALAR";
130}
131
132std::string ScalarType::getVtsScalarType() const {
133 static const char * const kName[] = {
134 "bool_t",
135 "int8_t",
136 "uint8_t",
137 "int16_t",
138 "uint16_t",
139 "int32_t",
140 "uint32_t",
141 "int64_t",
142 "uint64_t",
143 "float_t",
144 "double_t"
145 };
146
147 return kName[mKind];
148}
149
Andreas Huber881227d2016-08-02 14:20:21 -0700150void ScalarType::emitReaderWriter(
151 Formatter &out,
152 const std::string &name,
153 const std::string &parcelObj,
154 bool parcelObjIsPointer,
155 bool isReader,
156 ErrorMode mode) const {
Andreas Huber737080b2016-08-02 15:38:04 -0700157 emitReaderWriterWithCast(
158 out,
159 name,
160 parcelObj,
161 parcelObjIsPointer,
162 isReader,
163 mode,
164 false /* needsCast */);
165}
166
167void ScalarType::emitReaderWriterWithCast(
168 Formatter &out,
169 const std::string &name,
170 const std::string &parcelObj,
171 bool parcelObjIsPointer,
172 bool isReader,
173 ErrorMode mode,
174 bool needsCast) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700175 static const char *const kSuffix[] = {
Iliyan Malcheve0b28672016-08-14 13:35:12 -0700176 "Bool",
Andreas Huber881227d2016-08-02 14:20:21 -0700177 "Int8",
178 "Uint8",
179 "Int16",
180 "Uint16",
181 "Int32",
182 "Uint32",
183 "Int64",
184 "Uint64",
185 "Float",
186 "Double"
187 };
188
189 const std::string parcelObjDeref =
190 parcelObj + (parcelObjIsPointer ? "->" : ".");
191
Iliyan Malchev549e2592016-08-10 08:59:12 -0700192 out << "_hidl_err = "
Andreas Huber881227d2016-08-02 14:20:21 -0700193 << parcelObjDeref
194 << (isReader ? "read" : "write")
195 << kSuffix[mKind]
196 << "(";
197
Andreas Huber737080b2016-08-02 15:38:04 -0700198 if (needsCast) {
Andreas Huber737080b2016-08-02 15:38:04 -0700199 out << "("
Yifan Hong3b320f82016-11-01 15:15:54 -0700200 << getCppStackType()
Andreas Huber737080b2016-08-02 15:38:04 -0700201 << (isReader ? " *)" : ")");
202 }
203
Andreas Huber881227d2016-08-02 14:20:21 -0700204 if (isReader) {
205 out << "&";
206 }
207
208 out << name
209 << ");\n";
210
211 handleError(out, mode);
212}
213
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800214void ScalarType::emitHexDump(
215 Formatter &out,
216 const std::string &streamName,
217 const std::string &name) const {
218 out << streamName << " += toHexString(" << name << ");\n";
219}
220
Andreas Huber85eabdb2016-08-25 11:24:49 -0700221void ScalarType::emitJavaFieldReaderWriter(
222 Formatter &out,
Andreas Huber4c865b72016-09-14 15:26:27 -0700223 size_t /* depth */,
Andreas Huber709b62d2016-09-19 11:21:18 -0700224 const std::string & /* parcelName */,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700225 const std::string &blobName,
226 const std::string &fieldName,
227 const std::string &offset,
228 bool isReader) const {
229 if (isReader) {
230 out << fieldName
231 << " = "
232 << blobName
233 << ".get"
234 << getJavaSuffix()
235 << "("
236 << offset
237 << ");\n";
238
239 return;
240 }
241
242 out << blobName
243 << ".put"
244 << getJavaSuffix()
245 << "("
246 << offset
247 << ", "
248 << fieldName
249 << ");\n";
250}
251
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700252status_t ScalarType::emitVtsTypeDeclarations(Formatter &out) const {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700253 out << "type: " << getVtsType() << "\n";
254 out << "scalar_type: \"" << getVtsScalarType() << "\"\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700255 return OK;
256}
257
Andreas Huber85eabdb2016-08-25 11:24:49 -0700258void ScalarType::getAlignmentAndSize(size_t *align, size_t *size) const {
259 static const size_t kAlign[] = {
260 1, // bool, this is NOT standardized!
Andreas Huber85eabdb2016-08-25 11:24:49 -0700261 1, // int8_t
262 1, // uint8_t
263 2, // int16_t
264 2, // uint16_t
265 4, // int32_t
266 4, // uint32_t
267 8, // int64_t
268 8, // uint64_t
269 4, // float
270 8 // double
271 };
272
273 *align = *size = kAlign[mKind];
274}
275
Yifan Hong57886972016-08-17 10:42:15 -0700276ScalarType::Kind ScalarType::getKind() const {
277 return mKind;
278}
279
Andreas Huberc9410c72016-07-28 12:18:40 -0700280} // namespace android
281