blob: fccebd630d4b071ac4ad27791917102e7c6081b5 [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 Moreland30bb6a82016-11-30 09:18:34 -080044std::string ScalarType::typeName() const {
45 return getCppStackType();
46}
47
Yifan Hong3b320f82016-11-01 15:15:54 -070048std::string ScalarType::getCppType(StorageMode, bool) const {
Andreas Huber881227d2016-08-02 14:20:21 -070049 static const char *const kName[] = {
Andreas Huber881227d2016-08-02 14:20:21 -070050 "bool",
Andreas Huber881227d2016-08-02 14:20:21 -070051 "int8_t",
52 "uint8_t",
53 "int16_t",
54 "uint16_t",
55 "int32_t",
56 "uint32_t",
57 "int64_t",
58 "uint64_t",
59 "float",
60 "double"
61 };
62
Andreas Huber881227d2016-08-02 14:20:21 -070063 return kName[mKind];
64}
65
Yifan Hong4ed13472016-11-02 10:44:11 -070066std::string ScalarType::getJavaType(bool /* forInitializer */) const {
Andreas Huber2831d512016-08-15 09:33:47 -070067 static const char *const kName[] = {
Andreas Huber2831d512016-08-15 09:33:47 -070068 "boolean",
Andreas Huber2831d512016-08-15 09:33:47 -070069 "byte",
70 "byte",
71 "short",
72 "short",
73 "int",
74 "int",
75 "long",
76 "long",
77 "float",
78 "double"
79 };
80
81 return kName[mKind];
82}
83
Andreas Huber85eabdb2016-08-25 11:24:49 -070084std::string ScalarType::getJavaWrapperType() const {
85 static const char *const kName[] = {
86 "Boolean",
Andreas Huber85eabdb2016-08-25 11:24:49 -070087 "Byte",
88 "Byte",
89 "Short",
90 "Short",
Andreas Huber5001d932016-09-14 09:09:09 -070091 "Integer",
92 "Integer",
Andreas Huber85eabdb2016-08-25 11:24:49 -070093 "Long",
94 "Long",
95 "Float",
96 "Double"
97 };
98
99 return kName[mKind];
100}
101
Andreas Huber2831d512016-08-15 09:33:47 -0700102std::string ScalarType::getJavaSuffix() const {
103 static const char *const kSuffix[] = {
Andreas Huberd85a5092016-08-25 09:51:52 -0700104 "Bool",
Andreas Huber2831d512016-08-15 09:33:47 -0700105 "Int8",
106 "Int8",
107 "Int16",
108 "Int16",
109 "Int32",
110 "Int32",
111 "Int64",
112 "Int64",
113 "Float",
114 "Double"
115 };
116
117 return kSuffix[mKind];
118}
119
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700120std::string ScalarType::getVtsType() const {
121 return "TYPE_SCALAR";
122}
123
124std::string ScalarType::getVtsScalarType() const {
125 static const char * const kName[] = {
126 "bool_t",
127 "int8_t",
128 "uint8_t",
129 "int16_t",
130 "uint16_t",
131 "int32_t",
132 "uint32_t",
133 "int64_t",
134 "uint64_t",
135 "float_t",
136 "double_t"
137 };
138
139 return kName[mKind];
140}
141
Andreas Huber881227d2016-08-02 14:20:21 -0700142void ScalarType::emitReaderWriter(
143 Formatter &out,
144 const std::string &name,
145 const std::string &parcelObj,
146 bool parcelObjIsPointer,
147 bool isReader,
148 ErrorMode mode) const {
Andreas Huber737080b2016-08-02 15:38:04 -0700149 emitReaderWriterWithCast(
150 out,
151 name,
152 parcelObj,
153 parcelObjIsPointer,
154 isReader,
155 mode,
156 false /* needsCast */);
157}
158
159void ScalarType::emitReaderWriterWithCast(
160 Formatter &out,
161 const std::string &name,
162 const std::string &parcelObj,
163 bool parcelObjIsPointer,
164 bool isReader,
165 ErrorMode mode,
166 bool needsCast) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700167 static const char *const kSuffix[] = {
Iliyan Malcheve0b28672016-08-14 13:35:12 -0700168 "Bool",
Andreas Huber881227d2016-08-02 14:20:21 -0700169 "Int8",
170 "Uint8",
171 "Int16",
172 "Uint16",
173 "Int32",
174 "Uint32",
175 "Int64",
176 "Uint64",
177 "Float",
178 "Double"
179 };
180
181 const std::string parcelObjDeref =
182 parcelObj + (parcelObjIsPointer ? "->" : ".");
183
Iliyan Malchev549e2592016-08-10 08:59:12 -0700184 out << "_hidl_err = "
Andreas Huber881227d2016-08-02 14:20:21 -0700185 << parcelObjDeref
186 << (isReader ? "read" : "write")
187 << kSuffix[mKind]
188 << "(";
189
Andreas Huber737080b2016-08-02 15:38:04 -0700190 if (needsCast) {
Andreas Huber737080b2016-08-02 15:38:04 -0700191 out << "("
Yifan Hong3b320f82016-11-01 15:15:54 -0700192 << getCppStackType()
Andreas Huber737080b2016-08-02 15:38:04 -0700193 << (isReader ? " *)" : ")");
194 }
195
Andreas Huber881227d2016-08-02 14:20:21 -0700196 if (isReader) {
197 out << "&";
198 }
199
200 out << name
201 << ");\n";
202
203 handleError(out, mode);
204}
205
Andreas Huber85eabdb2016-08-25 11:24:49 -0700206void ScalarType::emitJavaFieldReaderWriter(
207 Formatter &out,
Andreas Huber4c865b72016-09-14 15:26:27 -0700208 size_t /* depth */,
Andreas Huber709b62d2016-09-19 11:21:18 -0700209 const std::string & /* parcelName */,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700210 const std::string &blobName,
211 const std::string &fieldName,
212 const std::string &offset,
213 bool isReader) const {
214 if (isReader) {
215 out << fieldName
216 << " = "
217 << blobName
218 << ".get"
219 << getJavaSuffix()
220 << "("
221 << offset
222 << ");\n";
223
224 return;
225 }
226
227 out << blobName
228 << ".put"
229 << getJavaSuffix()
230 << "("
231 << offset
232 << ", "
233 << fieldName
234 << ");\n";
235}
236
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700237status_t ScalarType::emitVtsTypeDeclarations(Formatter &out) const {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700238 out << "type: " << getVtsType() << "\n";
239 out << "scalar_type: \"" << getVtsScalarType() << "\"\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700240 return OK;
241}
242
Andreas Huber85eabdb2016-08-25 11:24:49 -0700243void ScalarType::getAlignmentAndSize(size_t *align, size_t *size) const {
244 static const size_t kAlign[] = {
245 1, // bool, this is NOT standardized!
Andreas Huber85eabdb2016-08-25 11:24:49 -0700246 1, // int8_t
247 1, // uint8_t
248 2, // int16_t
249 2, // uint16_t
250 4, // int32_t
251 4, // uint32_t
252 8, // int64_t
253 8, // uint64_t
254 4, // float
255 8 // double
256 };
257
258 *align = *size = kAlign[mKind];
259}
260
Yifan Hong57886972016-08-17 10:42:15 -0700261ScalarType::Kind ScalarType::getKind() const {
262 return mKind;
263}
264
Andreas Huberc9410c72016-07-28 12:18:40 -0700265} // namespace android
266