blob: b89001f96d715317a74e980f53a6fabacfd4e489 [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
40std::string ScalarType::getCppType(StorageMode, std::string *extra, 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
55 extra->clear();
56
57 return kName[mKind];
58}
59
Andreas Huber4c865b72016-09-14 15:26:27 -070060std::string ScalarType::getJavaType(
61 std::string *extra, bool /* forInitializer */) const {
Andreas Huber2831d512016-08-15 09:33:47 -070062 static const char *const kName[] = {
Andreas Huber2831d512016-08-15 09:33:47 -070063 "boolean",
Andreas Huber2831d512016-08-15 09:33:47 -070064 "byte",
65 "byte",
66 "short",
67 "short",
68 "int",
69 "int",
70 "long",
71 "long",
72 "float",
73 "double"
74 };
75
Andreas Huber4c865b72016-09-14 15:26:27 -070076 extra->clear();
Andreas Huber2831d512016-08-15 09:33:47 -070077 return kName[mKind];
78}
79
Andreas Huber85eabdb2016-08-25 11:24:49 -070080std::string ScalarType::getJavaWrapperType() const {
81 static const char *const kName[] = {
82 "Boolean",
Andreas Huber85eabdb2016-08-25 11:24:49 -070083 "Byte",
84 "Byte",
85 "Short",
86 "Short",
Andreas Huber5001d932016-09-14 09:09:09 -070087 "Integer",
88 "Integer",
Andreas Huber85eabdb2016-08-25 11:24:49 -070089 "Long",
90 "Long",
91 "Float",
92 "Double"
93 };
94
95 return kName[mKind];
96}
97
Andreas Huber2831d512016-08-15 09:33:47 -070098std::string ScalarType::getJavaSuffix() const {
99 static const char *const kSuffix[] = {
Andreas Huberd85a5092016-08-25 09:51:52 -0700100 "Bool",
Andreas Huber2831d512016-08-15 09:33:47 -0700101 "Int8",
102 "Int8",
103 "Int16",
104 "Int16",
105 "Int32",
106 "Int32",
107 "Int64",
108 "Int64",
109 "Float",
110 "Double"
111 };
112
113 return kSuffix[mKind];
114}
115
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700116std::string ScalarType::getVtsType() const {
117 return "TYPE_SCALAR";
118}
119
120std::string ScalarType::getVtsScalarType() const {
121 static const char * const kName[] = {
122 "bool_t",
123 "int8_t",
124 "uint8_t",
125 "int16_t",
126 "uint16_t",
127 "int32_t",
128 "uint32_t",
129 "int64_t",
130 "uint64_t",
131 "float_t",
132 "double_t"
133 };
134
135 return kName[mKind];
136}
137
Andreas Huber881227d2016-08-02 14:20:21 -0700138void ScalarType::emitReaderWriter(
139 Formatter &out,
140 const std::string &name,
141 const std::string &parcelObj,
142 bool parcelObjIsPointer,
143 bool isReader,
144 ErrorMode mode) const {
Andreas Huber737080b2016-08-02 15:38:04 -0700145 emitReaderWriterWithCast(
146 out,
147 name,
148 parcelObj,
149 parcelObjIsPointer,
150 isReader,
151 mode,
152 false /* needsCast */);
153}
154
155void ScalarType::emitReaderWriterWithCast(
156 Formatter &out,
157 const std::string &name,
158 const std::string &parcelObj,
159 bool parcelObjIsPointer,
160 bool isReader,
161 ErrorMode mode,
162 bool needsCast) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700163 static const char *const kSuffix[] = {
Iliyan Malcheve0b28672016-08-14 13:35:12 -0700164 "Bool",
Andreas Huber881227d2016-08-02 14:20:21 -0700165 "Int8",
166 "Uint8",
167 "Int16",
168 "Uint16",
169 "Int32",
170 "Uint32",
171 "Int64",
172 "Uint64",
173 "Float",
174 "Double"
175 };
176
177 const std::string parcelObjDeref =
178 parcelObj + (parcelObjIsPointer ? "->" : ".");
179
Iliyan Malchev549e2592016-08-10 08:59:12 -0700180 out << "_hidl_err = "
Andreas Huber881227d2016-08-02 14:20:21 -0700181 << parcelObjDeref
182 << (isReader ? "read" : "write")
183 << kSuffix[mKind]
184 << "(";
185
Andreas Huber737080b2016-08-02 15:38:04 -0700186 if (needsCast) {
187 std::string extra;
188
189 out << "("
190 << Type::getCppType(&extra)
191 << (isReader ? " *)" : ")");
192 }
193
Andreas Huber881227d2016-08-02 14:20:21 -0700194 if (isReader) {
195 out << "&";
196 }
197
198 out << name
199 << ");\n";
200
201 handleError(out, mode);
202}
203
Andreas Huber85eabdb2016-08-25 11:24:49 -0700204void ScalarType::emitJavaFieldReaderWriter(
205 Formatter &out,
Andreas Huber4c865b72016-09-14 15:26:27 -0700206 size_t /* depth */,
Andreas Huber709b62d2016-09-19 11:21:18 -0700207 const std::string & /* parcelName */,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700208 const std::string &blobName,
209 const std::string &fieldName,
210 const std::string &offset,
211 bool isReader) const {
212 if (isReader) {
213 out << fieldName
214 << " = "
215 << blobName
216 << ".get"
217 << getJavaSuffix()
218 << "("
219 << offset
220 << ");\n";
221
222 return;
223 }
224
225 out << blobName
226 << ".put"
227 << getJavaSuffix()
228 << "("
229 << offset
230 << ", "
231 << fieldName
232 << ");\n";
233}
234
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700235status_t ScalarType::emitVtsTypeDeclarations(Formatter &out) const {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700236 out << "type: " << getVtsType() << "\n";
237 out << "scalar_type: \"" << getVtsScalarType() << "\"\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700238 return OK;
239}
240
Andreas Huber85eabdb2016-08-25 11:24:49 -0700241void ScalarType::getAlignmentAndSize(size_t *align, size_t *size) const {
242 static const size_t kAlign[] = {
243 1, // bool, this is NOT standardized!
Andreas Huber85eabdb2016-08-25 11:24:49 -0700244 1, // int8_t
245 1, // uint8_t
246 2, // int16_t
247 2, // uint16_t
248 4, // int32_t
249 4, // uint32_t
250 8, // int64_t
251 8, // uint64_t
252 4, // float
253 8 // double
254 };
255
256 *align = *size = kAlign[mKind];
257}
258
Yifan Hong57886972016-08-17 10:42:15 -0700259ScalarType::Kind ScalarType::getKind() const {
260 return mKind;
261}
262
Andreas Huberc9410c72016-07-28 12:18:40 -0700263} // namespace android
264