blob: f537310ea122b477e2d87ffc33c557cff3b65323 [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 Hong3b320f82016-11-01 15:15:54 -070040std::string ScalarType::getCppType(StorageMode, 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
Andreas Huber881227d2016-08-02 14:20:21 -070055 return kName[mKind];
56}
57
Andreas Huber4c865b72016-09-14 15:26:27 -070058std::string ScalarType::getJavaType(
59 std::string *extra, bool /* forInitializer */) const {
Andreas Huber2831d512016-08-15 09:33:47 -070060 static const char *const kName[] = {
Andreas Huber2831d512016-08-15 09:33:47 -070061 "boolean",
Andreas Huber2831d512016-08-15 09:33:47 -070062 "byte",
63 "byte",
64 "short",
65 "short",
66 "int",
67 "int",
68 "long",
69 "long",
70 "float",
71 "double"
72 };
73
Andreas Huber4c865b72016-09-14 15:26:27 -070074 extra->clear();
Andreas Huber2831d512016-08-15 09:33:47 -070075 return kName[mKind];
76}
77
Andreas Huber85eabdb2016-08-25 11:24:49 -070078std::string ScalarType::getJavaWrapperType() const {
79 static const char *const kName[] = {
80 "Boolean",
Andreas Huber85eabdb2016-08-25 11:24:49 -070081 "Byte",
82 "Byte",
83 "Short",
84 "Short",
Andreas Huber5001d932016-09-14 09:09:09 -070085 "Integer",
86 "Integer",
Andreas Huber85eabdb2016-08-25 11:24:49 -070087 "Long",
88 "Long",
89 "Float",
90 "Double"
91 };
92
93 return kName[mKind];
94}
95
Andreas Huber2831d512016-08-15 09:33:47 -070096std::string ScalarType::getJavaSuffix() const {
97 static const char *const kSuffix[] = {
Andreas Huberd85a5092016-08-25 09:51:52 -070098 "Bool",
Andreas Huber2831d512016-08-15 09:33:47 -070099 "Int8",
100 "Int8",
101 "Int16",
102 "Int16",
103 "Int32",
104 "Int32",
105 "Int64",
106 "Int64",
107 "Float",
108 "Double"
109 };
110
111 return kSuffix[mKind];
112}
113
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700114std::string ScalarType::getVtsType() const {
115 return "TYPE_SCALAR";
116}
117
118std::string ScalarType::getVtsScalarType() const {
119 static const char * const kName[] = {
120 "bool_t",
121 "int8_t",
122 "uint8_t",
123 "int16_t",
124 "uint16_t",
125 "int32_t",
126 "uint32_t",
127 "int64_t",
128 "uint64_t",
129 "float_t",
130 "double_t"
131 };
132
133 return kName[mKind];
134}
135
Andreas Huber881227d2016-08-02 14:20:21 -0700136void ScalarType::emitReaderWriter(
137 Formatter &out,
138 const std::string &name,
139 const std::string &parcelObj,
140 bool parcelObjIsPointer,
141 bool isReader,
142 ErrorMode mode) const {
Andreas Huber737080b2016-08-02 15:38:04 -0700143 emitReaderWriterWithCast(
144 out,
145 name,
146 parcelObj,
147 parcelObjIsPointer,
148 isReader,
149 mode,
150 false /* needsCast */);
151}
152
153void ScalarType::emitReaderWriterWithCast(
154 Formatter &out,
155 const std::string &name,
156 const std::string &parcelObj,
157 bool parcelObjIsPointer,
158 bool isReader,
159 ErrorMode mode,
160 bool needsCast) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700161 static const char *const kSuffix[] = {
Iliyan Malcheve0b28672016-08-14 13:35:12 -0700162 "Bool",
Andreas Huber881227d2016-08-02 14:20:21 -0700163 "Int8",
164 "Uint8",
165 "Int16",
166 "Uint16",
167 "Int32",
168 "Uint32",
169 "Int64",
170 "Uint64",
171 "Float",
172 "Double"
173 };
174
175 const std::string parcelObjDeref =
176 parcelObj + (parcelObjIsPointer ? "->" : ".");
177
Iliyan Malchev549e2592016-08-10 08:59:12 -0700178 out << "_hidl_err = "
Andreas Huber881227d2016-08-02 14:20:21 -0700179 << parcelObjDeref
180 << (isReader ? "read" : "write")
181 << kSuffix[mKind]
182 << "(";
183
Andreas Huber737080b2016-08-02 15:38:04 -0700184 if (needsCast) {
Andreas Huber737080b2016-08-02 15:38:04 -0700185 out << "("
Yifan Hong3b320f82016-11-01 15:15:54 -0700186 << getCppStackType()
Andreas Huber737080b2016-08-02 15:38:04 -0700187 << (isReader ? " *)" : ")");
188 }
189
Andreas Huber881227d2016-08-02 14:20:21 -0700190 if (isReader) {
191 out << "&";
192 }
193
194 out << name
195 << ");\n";
196
197 handleError(out, mode);
198}
199
Andreas Huber85eabdb2016-08-25 11:24:49 -0700200void ScalarType::emitJavaFieldReaderWriter(
201 Formatter &out,
Andreas Huber4c865b72016-09-14 15:26:27 -0700202 size_t /* depth */,
Andreas Huber709b62d2016-09-19 11:21:18 -0700203 const std::string & /* parcelName */,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700204 const std::string &blobName,
205 const std::string &fieldName,
206 const std::string &offset,
207 bool isReader) const {
208 if (isReader) {
209 out << fieldName
210 << " = "
211 << blobName
212 << ".get"
213 << getJavaSuffix()
214 << "("
215 << offset
216 << ");\n";
217
218 return;
219 }
220
221 out << blobName
222 << ".put"
223 << getJavaSuffix()
224 << "("
225 << offset
226 << ", "
227 << fieldName
228 << ");\n";
229}
230
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700231status_t ScalarType::emitVtsTypeDeclarations(Formatter &out) const {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700232 out << "type: " << getVtsType() << "\n";
233 out << "scalar_type: \"" << getVtsScalarType() << "\"\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700234 return OK;
235}
236
Andreas Huber85eabdb2016-08-25 11:24:49 -0700237void ScalarType::getAlignmentAndSize(size_t *align, size_t *size) const {
238 static const size_t kAlign[] = {
239 1, // bool, this is NOT standardized!
Andreas Huber85eabdb2016-08-25 11:24:49 -0700240 1, // int8_t
241 1, // uint8_t
242 2, // int16_t
243 2, // uint16_t
244 4, // int32_t
245 4, // uint32_t
246 8, // int64_t
247 8, // uint64_t
248 4, // float
249 8 // double
250 };
251
252 *align = *size = kAlign[mKind];
253}
254
Yifan Hong57886972016-08-17 10:42:15 -0700255ScalarType::Kind ScalarType::getKind() const {
256 return mKind;
257}
258
Andreas Huberc9410c72016-07-28 12:18:40 -0700259} // namespace android
260